翼度科技»论坛 编程开发 .net 查看内容

【WinForm】 自学笔记二:连接数据库以及查询结果显示

9

主题

9

帖子

27

积分

新手上路

Rank: 1

积分
27
本次使用 SqlConnection 来连接数据库,使用 DataGridView 来显示查询的结果。最终效果如下:

 
 
一、连接数据库
1.获取连接数据库所需的字符串,包括服务器名称,数据库名称,用户名以及密码,可在配置文件中配置,或直接在代码里写死
在配置文件 App.config 中配置的代码:
  1. <configuration>
  2.   <appSettings>
  3.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  4.   </appSettings>
  5. </configuration>
复制代码
View Code2.获取数据库连接对象
  1. <configuration>
  2.   <appSettings>
  3.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  4.   </appSettings>
  5. </configuration>public static SqlConnection CreateSqlConn()<configuration>
  6.   <appSettings>
  7.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  8.   </appSettings>
  9. </configuration>{<configuration>
  10.   <appSettings>
  11.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  12.   </appSettings>
  13. </configuration>    //从配置文件 App.config 中获取数据库连接字符串<configuration>
  14.   <appSettings>
  15.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  16.   </appSettings>
  17. </configuration>    var conStr = ConfigurationManager.AppSettings["connectionstring"];<configuration>
  18.   <appSettings>
  19.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  20.   </appSettings>
  21. </configuration>    SqlConnection conn = new SqlConnection(conStr);<configuration>
  22.   <appSettings>
  23.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  24.   </appSettings>
  25. </configuration>    return conn;<configuration>
  26.   <appSettings>
  27.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  28.   </appSettings>
  29. </configuration>}
复制代码
View Code3.测试数据库的连通性
  1. <configuration>
  2.   <appSettings>
  3.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  4.   </appSettings>
  5. </configuration>private void button1_Click(object sender, EventArgs e)<configuration>
  6.   <appSettings>
  7.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  8.   </appSettings>
  9. </configuration>{<configuration>
  10.   <appSettings>
  11.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  12.   </appSettings>
  13. </configuration>    try<configuration>
  14.   <appSettings>
  15.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  16.   </appSettings>
  17. </configuration>    {<configuration>
  18.   <appSettings>
  19.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  20.   </appSettings>
  21. </configuration><configuration>
  22.   <appSettings>
  23.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  24.   </appSettings>
  25. </configuration>if (conn.State == ConnectionState.Closed)<configuration>
  26.   <appSettings>
  27.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  28.   </appSettings>
  29. </configuration><configuration>
  30.   <appSettings>
  31.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  32.   </appSettings>
  33. </configuration>{<configuration>
  34.   <appSettings>
  35.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  36.   </appSettings>
  37. </configuration><configuration>
  38.   <appSettings>
  39.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  40.   </appSettings>
  41. </configuration>    conn.Open();<configuration>
  42.   <appSettings>
  43.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  44.   </appSettings>
  45. </configuration><configuration>
  46.   <appSettings>
  47.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  48.   </appSettings>
  49. </configuration>}<configuration>
  50.   <appSettings>
  51.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  52.   </appSettings>
  53. </configuration><configuration>
  54.   <appSettings>
  55.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  56.   </appSettings>
  57. </configuration>MessageBox.Show("连接成功","数据库连接测试");<configuration>
  58.   <appSettings>
  59.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  60.   </appSettings>
  61. </configuration>    }<configuration>
  62.   <appSettings>
  63.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  64.   </appSettings>
  65. </configuration>    catch (Exception ex)<configuration>
  66.   <appSettings>
  67.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  68.   </appSettings>
  69. </configuration>    {<configuration>
  70.   <appSettings>
  71.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  72.   </appSettings>
  73. </configuration><configuration>
  74.   <appSettings>
  75.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  76.   </appSettings>
  77. </configuration>MessageBox.Show("连接失败:"+ ex.Message, "数据库连接测试");<configuration>
  78.   <appSettings>
  79.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  80.   </appSettings>
  81. </configuration>    }<configuration>
  82.   <appSettings>
  83.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  84.   </appSettings>
  85. </configuration>}
复制代码
View Code二、查询数据并在画面上显示
1.在窗口中添加 DataGridView 对象,用于显示数据

 
右键点击 DataGridView 对象,可以编辑要显示的列,以及表头的名称。
列属性:
DataPropertyName : 数据源中对应字段的名称,根据对应的字段来显示值
HeaderText : 表头显示的文字
DataGridView属性
AllowUserToAddRows :DataGridView默认底部有一行空数据,将改属性修改为False,可以隐藏空白行

 
  AutoSizeColumnsMode:控制列的宽度,将该属性ScrollBars修改为 Fill , 能让列的宽度自适应,充满整个画面
 ScrollBars : 控制滚动条的状态,可隐藏滚动条,或只显示水平或垂直的滚动条
2.生成数据查询SQL,并将查询结果绑定到 DataGridView 上,在画面进行显示
  1. <configuration>
  2.   <appSettings>
  3.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  4.   </appSettings>
  5. </configuration>private void button2_Click(object sender, EventArgs e)<configuration>
  6.   <appSettings>
  7.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  8.   </appSettings>
  9. </configuration>{<configuration>
  10.   <appSettings>
  11.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  12.   </appSettings>
  13. </configuration>    var dataCnt = numericUpDown1.Value;<configuration>
  14.   <appSettings>
  15.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  16.   </appSettings>
  17. </configuration>    dataCnt = dataCnt > 0 ? dataCnt : 5;<configuration>
  18.   <appSettings>
  19.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  20.   </appSettings>
  21. </configuration>    string sql = "Select Top " + dataCnt + " UserId,RedId,NickName,[Desc],Location  From UserInfo ";<configuration>
  22.   <appSettings>
  23.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  24.   </appSettings>
  25. </configuration>    var ds = Query(sql);<configuration>
  26.   <appSettings>
  27.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  28.   </appSettings>
  29. </configuration>    if (ds.Tables?.Count > 0)<configuration>
  30.   <appSettings>
  31.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  32.   </appSettings>
  33. </configuration>    {<configuration>
  34.   <appSettings>
  35.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  36.   </appSettings>
  37. </configuration><configuration>
  38.   <appSettings>
  39.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  40.   </appSettings>
  41. </configuration>var dt = ds.Tables[0];<configuration>
  42.   <appSettings>
  43.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  44.   </appSettings>
  45. </configuration><configuration>
  46.   <appSettings>
  47.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  48.   </appSettings>
  49. </configuration>//直接绑定DataTable<configuration>
  50.   <appSettings>
  51.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  52.   </appSettings>
  53. </configuration><configuration>
  54.   <appSettings>
  55.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  56.   </appSettings>
  57. </configuration>dataGridView1.DataSource = dt;<configuration>
  58.   <appSettings>
  59.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  60.   </appSettings>
  61. </configuration><configuration>
  62.   <appSettings>
  63.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  64.   </appSettings>
  65. </configuration>////绑定对象列表<configuration>
  66.   <appSettings>
  67.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  68.   </appSettings>
  69. </configuration><configuration>
  70.   <appSettings>
  71.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  72.   </appSettings>
  73. </configuration>//List userList = new List();<configuration>
  74.   <appSettings>
  75.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  76.   </appSettings>
  77. </configuration><configuration>
  78.   <appSettings>
  79.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  80.   </appSettings>
  81. </configuration>//if (dt.Rows?.Count > 0)<configuration>
  82.   <appSettings>
  83.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  84.   </appSettings>
  85. </configuration><configuration>
  86.   <appSettings>
  87.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  88.   </appSettings>
  89. </configuration>//{<configuration>
  90.   <appSettings>
  91.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  92.   </appSettings>
  93. </configuration><configuration>
  94.   <appSettings>
  95.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  96.   </appSettings>
  97. </configuration>//    foreach (DataRow row in dt.Rows)<configuration>
  98.   <appSettings>
  99.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  100.   </appSettings>
  101. </configuration><configuration>
  102.   <appSettings>
  103.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  104.   </appSettings>
  105. </configuration>//    {<configuration>
  106.   <appSettings>
  107.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  108.   </appSettings>
  109. </configuration><configuration>
  110.   <appSettings>
  111.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  112.   </appSettings>
  113. </configuration>//<configuration>
  114.   <appSettings>
  115.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  116.   </appSettings>
  117. </configuration>UserInfo user = new UserInfo<configuration>
  118.   <appSettings>
  119.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  120.   </appSettings>
  121. </configuration><configuration>
  122.   <appSettings>
  123.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  124.   </appSettings>
  125. </configuration>//<configuration>
  126.   <appSettings>
  127.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  128.   </appSettings>
  129. </configuration>{<configuration>
  130.   <appSettings>
  131.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  132.   </appSettings>
  133. </configuration><configuration>
  134.   <appSettings>
  135.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  136.   </appSettings>
  137. </configuration>//<configuration>
  138.   <appSettings>
  139.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  140.   </appSettings>
  141. </configuration>    UserId = row["UserId"] + "",<configuration>
  142.   <appSettings>
  143.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  144.   </appSettings>
  145. </configuration><configuration>
  146.   <appSettings>
  147.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  148.   </appSettings>
  149. </configuration>//<configuration>
  150.   <appSettings>
  151.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  152.   </appSettings>
  153. </configuration>    RedId = row["RedId"] + "",<configuration>
  154.   <appSettings>
  155.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  156.   </appSettings>
  157. </configuration><configuration>
  158.   <appSettings>
  159.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  160.   </appSettings>
  161. </configuration>//<configuration>
  162.   <appSettings>
  163.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  164.   </appSettings>
  165. </configuration>    NickName = row["NickName"] + "",<configuration>
  166.   <appSettings>
  167.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  168.   </appSettings>
  169. </configuration><configuration>
  170.   <appSettings>
  171.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  172.   </appSettings>
  173. </configuration>//<configuration>
  174.   <appSettings>
  175.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  176.   </appSettings>
  177. </configuration>    Desc = row["Desc"] + "",<configuration>
  178.   <appSettings>
  179.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  180.   </appSettings>
  181. </configuration><configuration>
  182.   <appSettings>
  183.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  184.   </appSettings>
  185. </configuration>//<configuration>
  186.   <appSettings>
  187.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  188.   </appSettings>
  189. </configuration>    Location = row["Location"] + "",<configuration>
  190.   <appSettings>
  191.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  192.   </appSettings>
  193. </configuration><configuration>
  194.   <appSettings>
  195.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  196.   </appSettings>
  197. </configuration>//<configuration>
  198.   <appSettings>
  199.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  200.   </appSettings>
  201. </configuration>};<configuration>
  202.   <appSettings>
  203.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  204.   </appSettings>
  205. </configuration><configuration>
  206.   <appSettings>
  207.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  208.   </appSettings>
  209. </configuration>//<configuration>
  210.   <appSettings>
  211.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  212.   </appSettings>
  213. </configuration>userList.Add(user);<configuration>
  214.   <appSettings>
  215.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  216.   </appSettings>
  217. </configuration><configuration>
  218.   <appSettings>
  219.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  220.   </appSettings>
  221. </configuration>//    }<configuration>
  222.   <appSettings>
  223.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  224.   </appSettings>
  225. </configuration><configuration>
  226.   <appSettings>
  227.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  228.   </appSettings>
  229. </configuration>//}<configuration>
  230.   <appSettings>
  231.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  232.   </appSettings>
  233. </configuration><configuration>
  234.   <appSettings>
  235.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  236.   </appSettings>
  237. </configuration>//dataGridView1.DataSource = userList;<configuration>
  238.   <appSettings>
  239.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  240.   </appSettings>
  241. </configuration>    }<configuration>
  242.   <appSettings>
  243.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  244.   </appSettings>
  245. </configuration>}<configuration>
  246.   <appSettings>
  247.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  248.   </appSettings>
  249. </configuration>public DataSet Query(string sqlStr)<configuration>
  250.   <appSettings>
  251.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  252.   </appSettings>
  253. </configuration>{<configuration>
  254.   <appSettings>
  255.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  256.   </appSettings>
  257. </configuration>    DataSet ds = new DataSet();<configuration>
  258.   <appSettings>
  259.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  260.   </appSettings>
  261. </configuration>    try<configuration>
  262.   <appSettings>
  263.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  264.   </appSettings>
  265. </configuration>    {<configuration>
  266.   <appSettings>
  267.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  268.   </appSettings>
  269. </configuration><configuration>
  270.   <appSettings>
  271.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  272.   </appSettings>
  273. </configuration>if (conn.State == ConnectionState.Closed)<configuration>
  274.   <appSettings>
  275.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  276.   </appSettings>
  277. </configuration><configuration>
  278.   <appSettings>
  279.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  280.   </appSettings>
  281. </configuration>{<configuration>
  282.   <appSettings>
  283.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  284.   </appSettings>
  285. </configuration><configuration>
  286.   <appSettings>
  287.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  288.   </appSettings>
  289. </configuration>    conn.Open();<configuration>
  290.   <appSettings>
  291.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  292.   </appSettings>
  293. </configuration><configuration>
  294.   <appSettings>
  295.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  296.   </appSettings>
  297. </configuration>}<configuration>
  298.   <appSettings>
  299.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  300.   </appSettings>
  301. </configuration><configuration>
  302.   <appSettings>
  303.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  304.   </appSettings>
  305. </configuration>SqlDataAdapter command = new SqlDataAdapter(sqlStr, conn);<configuration>
  306.   <appSettings>
  307.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  308.   </appSettings>
  309. </configuration><configuration>
  310.   <appSettings>
  311.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  312.   </appSettings>
  313. </configuration>command.Fill(ds);<configuration>
  314.   <appSettings>
  315.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  316.   </appSettings>
  317. </configuration>    }<configuration>
  318.   <appSettings>
  319.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  320.   </appSettings>
  321. </configuration>    catch (Exception ex)<configuration>
  322.   <appSettings>
  323.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  324.   </appSettings>
  325. </configuration>    {<configuration>
  326.   <appSettings>
  327.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  328.   </appSettings>
  329. </configuration><configuration>
  330.   <appSettings>
  331.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  332.   </appSettings>
  333. </configuration>throw new Exception(ex.Message);<configuration>
  334.   <appSettings>
  335.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  336.   </appSettings>
  337. </configuration>    }<configuration>
  338.   <appSettings>
  339.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  340.   </appSettings>
  341. </configuration>    return ds;<configuration>
  342.   <appSettings>
  343.     <add key="connectionstring" value="server=192.168.3.201;database=Nine_Xhs;uid=sa;pwd=Nine!2021;"/>
  344.   </appSettings>
  345. </configuration>}
复制代码
View Code注:DataGridView 对象绑定的数据源,DataTable 以及 对象List 都是可以的,显示的效果是一样的
 

来源:https://www.cnblogs.com/SuperJason/archive/2023/03/09/17201062.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具