js 连接数据库
1,首先创建ODBC数据源。
2,确保IE的的安全设置“通过域访问数据源”启用。
<script language="javascript">
//用 javascript 写服务器端连接数据库的代码示例
var conn = new ActiveXObject("ADODB.Connection");
conn.Open("Provider=SQLOLEDB.1; Data Source=.; User ID=sa; " + "Password=qaz123$%^; Initial Catalog=Vancl_Union");
var rs = new ActiveXObject("ADODB.Recordset");
var sql = "select * from dbo.Channel";
rs.open(sql, conn);
alert(rs(0)); //取出第一个来
var id = "";
var name="";
while (!rs.EOF) {
// id+= rs.Fields("id");
name += rs.Fields("name");
rs.MoveNext;
}
// document.writeln(id);
document.writeln(name);
rs.close();
rs = null;
conn.close();
conn = null;
</script>
function Insert()
{
var con = new ActiveXObject("ADODB.Connection");
//连接字符串
//var str = "DRIVER={SQL SERVER};SERVER=.;uid=sa;pwd=qaz123$%^;database=Test";
var str = "Provider=SQLOLEDB.1; Data Source=.; User ID=sa; " + "Password=qaz123$%^; Initial Catalog=Test";
//打开数据源
con.Open(str);
var sql = "insert into Test(Name,Email) values('11','11')";
// //执行sql语句
con.Execute(sql);
}