zoukankan      html  css  js  c++  java
  • 9月15日

    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里的表名

    作者:wpf之家
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    ES6 变量的解构赋值
    【js重学系列】new
    【js面试系列】手写常见js方法
    【js重学系列】this
    js-继承
    【js重学系列】数组高阶函数
    【js面试系列】数组去重
    云服务器部署项目-基本使用流程
    mongodb-基本使用
    移动端适配
  • 原文地址:https://www.cnblogs.com/wpf123/p/2347478.html
Copyright © 2011-2022 走看看