当前位置:Gxlcms > 数据库问题 > C#—连接SQLserver数据库,并执行查询语句代码

C#—连接SQLserver数据库,并执行查询语句代码

时间:2021-07-01 10:21:17 帮助过:149人阅读

//字段ArticleID,ArticleName,ArticleNumber,Unit,Weight,Price,Currency,IsIuggage,IsQuarantine
string str = myArticleInfo[i].ArticleID.ToString();//取物品编码进行查询
 string strconn = "Data Source=192.168.100.132;Initial Catalog=CustomsPlatform;User ID=sa;password=max_123456";  //链接语句

 SqlConnection conn = new SqlConnection(strconn);    ////连接到引入的数据库
conn.Open();// 打开数据库连接
SqlCommand sqlcmd = new SqlCommand();//创建 Command 对象
sqlcmd.CommandText = string.Format(@"select ArticleID,ArticleName,ArticleNumber,Unit,Weight,Price,Currency,IsIuggage,IsQuarantine 
                                                        from [CustomsPlatform].[dbo].[TB_ArticleInfo] where ArticleID = ‘{0}‘", str);//执行查询语句
sqlcmd.Connection = conn;
SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
 DataTable dt = new System.Data.DataTable();
adp.Fill(dt);
this.SortListViewData.DataSource = dt;
conn.Close();// 关闭数据库连接

  

C#—连接SQLserver数据库,并执行查询语句代码

标签:tab   ice   inf   catalog   init   sel   ble   spl   sql   

人气教程排行