C#连接sqlserver与工具类

sqlServer连接类

internal class Db: IDisposable
    {
        private SqlConnection connection;
//
        public Db() { 
            //connection= new SqlConnection(@"server=SERVER;database=DATABASE;Trusted_Connection=SSPI");
            connection = new SqlConnection(@"server=SERVER;database=DATABASE;uid=uid;pwd=pwd");
            connection.Open();
        }

        public DataTable getBySql(string sql)
        {
            DataTable table= new DataTable();
            SqlDataAdapter sqlDataAdapter=new SqlDataAdapter(new SqlCommand(sql,connection));
            sqlDataAdapter.Fill(table);
            return table;
        }


        public void setBySql(string sql)
        {
            new SqlCommand(sql,connection).ExecuteNonQuery();
        }

        public void Dispose()
        {
            connection.Close();
        }
    }

工具类Util

    internal class util
    {
        //跨界面数据传输
        public static Dictionary<string,object> dict=new Dictionary<string,object>();

        //填充listview
        public static void fill( ListView listView ,DataTable table, string[] columns)
        {
            listView.Clear();
            listView.BeginUpdate();
            for(int i = 0; i < columns.Length; i++)
            {
                listView.Columns.Add(columns[i],listView.Width/columns.Length-1,HorizontalAlignment.Left);
            }
            for(int i = 0; i < table.Rows.Count; i++) {
                ListViewItem item=new ListViewItem();
                for(int j=0;j< table.Columns.Count; j++) {

                    if (j <= 0)
                    {
                        item.Text= table.Rows[i][j].ToString();
                    }
                    else
                    {
                        item.SubItems.Add(table.Rows[i][j].ToString());
                    }
                }
                listView.Items.Add(item);
            }
            listView.EndUpdate();
        }

        //填充下拉框
        public static void fill( ComboBox comboBox, DataTable table)
        {
            comboBox.Items.Clear();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Columns.Count; j++)
                {

                    comboBox.Items.Add(table.Rows[i][j]);
                }
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C# 工具类,用于连接 SQL Server 数据库: ```csharp using System; using System.Data.SqlClient; public class SqlHelper { // 连接字符串 private string connectionString; // 构造函数 public SqlHelper(string connectionString) { this.connectionString = connectionString; } // 执行 SQL 命令,返回受影响的行数 public int ExecuteNonQuery(string sql) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(sql, connection)) { return command.ExecuteNonQuery(); } } } // 执行 SQL 查询,返回 SqlDataReader 对象 public SqlDataReader ExecuteReader(string sql) { SqlConnection connection = new SqlConnection(connectionString); connection.Open(); SqlCommand command = new SqlCommand(sql, connection); SqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection); return reader; } // 执行 SQL 查询,返回单个结果 public object ExecuteScalar(string sql) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(sql, connection)) { return command.ExecuteScalar(); } } } } ``` 使用示例: ```csharp // 连接字符串 string connectionString = "Server=(local);Database=MyDatabase;User ID=MyUsername;Password=MyPassword;"; // 创建 SqlHelper 对象 SqlHelper sqlHelper = new SqlHelper(connectionString); // 执行 SQL 命令 sqlHelper.ExecuteNonQuery("INSERT INTO MyTable (Column1, Column2) VALUES ('Value1', 'Value2')"); // 执行 SQL 查询 SqlDataReader reader = sqlHelper.ExecuteReader("SELECT * FROM MyTable"); while (reader.Read()) { // 读取数据 string column1 = reader.GetString(0); string column2 = reader.GetString(1); } // 执行 SQL 查询,返回单个结果 object result = sqlHelper.ExecuteScalar("SELECT COUNT(*) FROM MyTable"); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值