数据库连接:直连式和断开式
using System.Data using System.sqlClient
connection创建和数据库的连接:1,服务器2,身份验证3,指定数据库
"."代表本机或(local),Database是指数据库名字,如果用Windows验证用Integrated security = true;
Sqlconnection conn = new Sqlconnection(); conn.ConnectionString = "server = .;Database = stu;uid = sa; pwd = 123";conn.Open();
command
//构造命令对象 Sqlcommand com = new Sqlcommand(); cmd.commandText = "select * from student"; cmd.connection = con;
datareade
//声明结果集 SqlDataReader dr = com.ExecuteReader(); //遍历dr数据 if(dr.HasRows) { whlie(dr.Read) { ListBox1.Item.Add(dr[1].Tostring()); } }con.Close();
全程手打的有错见谅
时间: 2024-10-13 09:45:57