数据库
首页 > 数据库> > Visual中SqlHelper的书写(*)

Visual中SqlHelper的书写(*)

作者:互联网

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Configuration;
6 using System.Data;
7 using System.Data.SqlClient;

插入引用

private static string strconn = ConfigurationManager.ConnectionStrings["DigitalProductShopConnectionString"].ConnectionString;

获取连接

 1  public static DataTable ChaXun(string sql)
 2         {
 3             using (DataTable dt=new DataTable())
 4             {
 5                 using (SqlDataAdapter sda=new SqlDataAdapter(sql,strconn))
 6                 {
 7                     sda.Fill(dt);
 8                 }
 9 
10                 return dt;
11             }
12         }

查询方法

 1  public static int FeiChaXun(string sql)
 2         {
 3             int i = 0;
 4             using (SqlConnection conn = new SqlConnection(strconn))
 5             {
 6                 using (SqlCommand scd = new SqlCommand(sql, conn))
 7                 {
 8                     try
 9                     {
10                         conn.Open();
11                         i = scd.ExecuteNonQuery();
12                     }
13                     finally
14                     {
15                         if (conn.State == ConnectionState.Open)
16                         {
17                             conn.Close();
18                         }
19                     }
20                 }
21             }
22             return i;
23         }

增删改方法

标签:SqlHelper,书写,sql,System,Visual,static,new,using,conn
来源: https://www.cnblogs.com/douanan/p/13157989.html