代码改变世界

9月15日

  观海看云  阅读(120)  评论(0编辑  收藏  举报

 

using System;
 
using System.Data;
 
using System.Data.SqlClient;
 
using System.Windows.Forms;
 
namespace winApplication
 
{
 
     public class sqlAccess
 
     {
 
         //与SQL Server的连接字符串设置
 
         private string _connString;
 
         private string _strSql;
 

 

         private SqlCommandBuilder sqlCmdBuilder;
 
         private DataSet ds = new DataSet();
 
         private SqlDataAdapter da;
 
         public sqlAccess(string connString,string strSql)
 
         {
 
              this._connString=connString;
 
         }
 

 

         private SqlConnection GetConn()
 
         {
 
              try
 
              {
 
                   SqlConnection Connection = new SqlConnection(this._connString);
 
                   Connection.Open();
 
                   return Connection;
 
              }
 
              catch (Exception ex)
 
              {
 
                   MessageBox.Show(ex.Message,"数据库连接失败");
 
                   throw;
 
              }
 
         }
 

 

         //根据输入的SQL语句检索数据库数据
 
         public DataSet SelectDb(string strSql,string strTableName)
 
         {
 
              try
 
              {
 
              this._strSql = strSql;
 
              this.da = new SqlDataAdapter(this._strSql,this.GetConn());
 
              this.ds.Clear();
 
              this.da.Fill(ds,strTableName);
 
              return ds;//返回填充了数据的DataSet,其中数据表以strTableName给出的字符串命名
 
              }
 
              catch (Exception ex)
 
              {
 
                   MessageBox.Show(ex.Message,"数据库操作失败");
 
                   throw;
 
              }
 
         }
 

 

         //数据库数据更新(传DataSet和DataTable的对象)
 
         public DataSet UpdateDs(DataSet changedDs,string tableName)
 
         {
 
              try
 
              {
 
              this.da = new SqlDataAdapter(this._strSql,this.GetConn());
 
              this.sqlCmdBuilder = new SqlCommandBuilder(da);
 
              this.da.Update(changedDs,tableName);
 
              changedDs.AcceptChanges();
 
              return changedDs;//返回更新了的数据库表
 
              }
 
              catch (Exception ex)
 
              {
 
                   MessageBox.Show(ex.Message,"数据库更新失败");
 
                   throw;
 
              }
 
                    }

 

 

 

public static void updatadata(DataSet srcDS, string tableName, string dsTable)
{
DataSet ds = new DataSet();
SqlConnection Connection = new SqlConnection(connectionString);
Connection.Open();
SqlDataAdapter da =new SqlDataAdapter("select * from " + tableName + "", Connection);
SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(da);
da.Update(srcDS, dsTable);
srcDS.AcceptChanges();
}



那里错了 这个方法传3个参数 一个是DATASET一个是要插入的表名
另一个是 DATASET里的表名

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示