c#怎样连接sql server数据库并循环读取里面的数据啊

2024-11-20 12:33:13
推荐回答(1个)
回答1:

头文件添加
using System.Data;
using System.Data.SqlClient;
创建链接

SqlConnection con = new SqlConnection();
con.ConnectionString = "server=服务器ip;database=数据库名;uid=数据库账户;pwd=密码";
con.Open();

写sql语句
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "sql语句";
SqlDataReader dr = com.ExecuteReader();//执行SQL语句
SqlDataReader dx = dr;//执行SQL语句
取数据
while (dr.Read())--类似指针
{ cs5 = String.Format("{0}_{1}_{2}", dr[9], dr[10], dr[11]) }