⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlhelper.cs

📁 commandBuilder应用数据库的使用
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace student.DB
{
    public class SqlHelper
    {
        private String connectionString = "server=.;database=stuDB;uid=sa;pwd=sa";
        private bool flag = false;

        /// <summary>
        /// 执行sql查询语句
        /// </summary>
        /// <param name="sql">sql查询语句</param>
        /// <returns>DataSet</returns>
        public DataSet getDataSet(String sql)
        {
            DataSet ds = new DataSet();
            using (SqlConnection con=new SqlConnection(connectionString))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(sql, con))
                {
                    try
                    {
                        da.Fill(ds);
                    }
                    catch (SqlException e)
                    {
                        throw new Exception(e.Message);
                    }
                }
            }
            return ds;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="ds">DataSet</param>
        /// <param name="sql">sql语句</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public bool doUpdate(DataSet ds,String sql,String tableName)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(sql, con))
                {
                    //数据库表一定要有主键列 否则此处通不过
                    SqlCommandBuilder builder = new SqlCommandBuilder(da);
                    try
                    {
                        lock (this)
                        {
                            da.Update(ds, tableName);
                            flag = true;
                       }
                    }
                    catch (SqlException e)
                    {
                        throw new Exception(e.Message);
                    }
                }
            }
            return flag;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -