asp查询调用SQL数据库代码
1个回答
展开全部
记得在最上面加上这句指令集using System.Data.SqlClient;
在后台代码中加入:
String constr = "server=.\\sqlexpress;database=test;uid=sa;pwd=123";这一种是sa用户连接的代码
//String constr = "server=.\\sqlexpress;database=test;Integrated security=true";这个是Windows身份验证连接的代码
SqlConnection con = new SqlConnection(constr);
con.Open();
// response.Write(con.State);
String sql = "select * from Student";
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Response.Write(dr[1].ToString()+""+dr["name"].ToString()+"<br>");//dr[2]=dr["name"] response.write(dr.getInt32(3)+"<br>")
}
dr.Close();
con.Close();
在后台代码中加入:
String constr = "server=.\\sqlexpress;database=test;uid=sa;pwd=123";这一种是sa用户连接的代码
//String constr = "server=.\\sqlexpress;database=test;Integrated security=true";这个是Windows身份验证连接的代码
SqlConnection con = new SqlConnection(constr);
con.Open();
// response.Write(con.State);
String sql = "select * from Student";
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Response.Write(dr[1].ToString()+""+dr["name"].ToString()+"<br>");//dr[2]=dr["name"] response.write(dr.getInt32(3)+"<br>")
}
dr.Close();
con.Close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询