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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//字段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();// 关闭数据库连接

  

作者:ganbei

出处:https://www.cnblogs.com/ganbei/p/11274548.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   干杯Archer  阅读(1797)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up light_mode palette
选择主题