当前位置: 编程技术>.net/c#/asp.net
c#连接sqlserver数据库的例子
来源: 互联网 发布时间:2014-08-30
本文导语: c#代码中连接sql server的写法: 代码示例: string strconn = "data source = (local);initial catalog = sxaic; user id = sa;password = 123456"; SqlConnection conn = new SqlConnection(); conn.ConnectionString = strconn; conn.Open(); string sql = "select * from ...
c#代码中连接sql server的写法:
代码示例:
string strconn = "data source = (local);initial catalog = sxaic; user id = sa;password = 123456";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = strconn;
conn.Open();
string sql = "select * from t_brand";
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
sqlAdapter.Fill(ds,"t_brand");
Label1.Text = ds.Tables[0].Rows[0]["brand"].ToString();
conn.Close();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = strconn;
conn.Open();
string sql = "select * from t_brand";
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
sqlAdapter.Fill(ds,"t_brand");
Label1.Text = ds.Tables[0].Rows[0]["brand"].ToString();
conn.Close();