C#—連接SQLserver數據庫,並執行查詢語句代碼


//字段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();// 關閉數據庫連接

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM