如何更新SQL Server数据库

private void Form1_Load(object sender, System.EventArgs e)
  {//显示所有记录
   this.sqlDataAdapter1.Fill(this.dataSet1,"Customers");
   this.dataGrid1.DataSource=this.dataSet1; 
   if(!this.dataGrid1.IsExpanded(0))
    this.dataGrid1.Expand(0);
  }

  private void button1_Click(object sender, System.EventArgs e)
  {//查询记录
   try
   {
    this.dataSet1.Clear();       
    string StrSQL="SELECT * FROM Customers WHERE ";
    StrSQL+=this.comboBox1.Text+" LIKE  '";
    StrSQL+=this.comboBox2.Text+"'";
    if(this.comboBox1.Text=="All")
     StrSQL="SELECT * FROM Customers";
    this.sqlDataAdapter1.SelectCommand.CommandText=StrSQL;
    this.sqlDataAdapter1.SelectCommand.Connection=this.sqlConnection1;
    //打开数据库连接
    this.sqlConnection1.Open();
    //执行SQL命令
    this.sqlDataAdapter1.SelectCommand.ExecuteNonQuery();
    //关闭连接
    this.sqlConnection1.Close();
    //更新数据库
    this.sqlDataAdapter1.Fill(this.dataSet1,"Customers");
    this.dataGrid1.DataSource=this.dataSet1; 
   }
   catch(Exception Err)
   {
    MessageBox.Show("查询数据库记录操作失败:"+Err.Message,"信息提示",
     MessageBoxButtons.OK,MessageBoxIcon.Information);
    //如果打开了连接,则关闭它
    if(this.sqlConnection1.State==ConnectionState.Open)
    {
     this.sqlConnection1.Close();
    }
   }    
  }

  private void Form1_Closed(object sender, System.EventArgs e)
  {//关闭程序
   //如果打开了连接,则关闭它
   if(this.sqlConnection1.State==ConnectionState.Open)
   {
    this.sqlConnection1.Close();
   }   
  }

  private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  {//显示查询值列表项
   this.dataSet2.Clear();       
   string  StrSQL="SELECT * FROM Customers";
   this.sqlDataAdapter1.SelectCommand.CommandText=StrSQL;
   this.sqlDataAdapter1.SelectCommand.Connection=this.sqlConnection1;
   //打开数据库连接
   this.sqlConnection1.Open();
   //执行SQL命令
   this.sqlDataAdapter1.SelectCommand.ExecuteNonQuery();
   //关闭连接
   this.sqlConnection1.Close();
   //更新数据库
   this.sqlDataAdapter1.Fill(this.dataSet2,"Customers");
   this.comboBox2.DataSource=this.dataSet2;
   if(this.comboBox1.Text=="CustomerID")
   {
    this.comboBox2.DisplayMember="Customers.CustomerID";
    this.comboBox2.ValueMember="Customers.CustomerID";
    return;
   }
   if(this.comboBox1.Text=="CompanyName")
   {
    this.comboBox2.DisplayMember="Customers.CompanyName";
    this.comboBox2.ValueMember="Customers.CompanyName";
    return;
   }
   if(this.comboBox1.Text=="ContactName")
   {
    this.comboBox2.DisplayMember="Customers.ContactName";
    this.comboBox2.ValueMember="Customers.ContactName";
    return;
   }
   if(this.comboBox1.Text=="ContactTitle")
   {
    this.comboBox2.DisplayMember="Customers.ContactTitle";
    this.comboBox2.ValueMember="Customers.ContactTitle";
    return;
   }
   if(this.comboBox1.Text=="Address")
   {
    this.comboBox2.DisplayMember="Customers.Address";
    this.comboBox2.ValueMember="Customers.Address";
    return;
   }
   if(this.comboBox1.Text=="City")
   {
    this.comboBox2.DisplayMember="Customers.City";
    this.comboBox2.ValueMember="Customers.City";
    return;
   }
   if(this.comboBox1.Text=="Region")
   {
    this.comboBox2.DisplayMember="Customers.Region";
    this.comboBox2.ValueMember="Customers.Region";
    return;
   }
   if(this.comboBox1.Text=="PostalCode")
   {
    this.comboBox2.DisplayMember="Customers.PostalCode";
    this.comboBox2.ValueMember="Customers.PostalCode";
    return;
   }
   if(this.comboBox1.Text=="Country")
   {
    this.comboBox2.DisplayMember="Customers.Country";
    this.comboBox2.ValueMember="Customers.Country";
    return;
   }
   if(this.comboBox1.Text=="Phone")
   {
    this.comboBox2.DisplayMember="Customers.Phone";
    this.comboBox2.ValueMember="Customers.Phone";
    return;
   }
   if(this.comboBox1.Text=="Fax")
   {
    this.comboBox2.DisplayMember="Customers.Fax";
    this.comboBox2.ValueMember="Customers.Fax";
    return;
   }  
  }

  private void button2_Click(object sender, System.EventArgs e)
  {//更新数据库记录
   try
   {
    DataSet MyDataSet=this.dataSet1.GetChanges();
    if(MyDataSet!=null)
    {
     int Rows=this.sqlDataAdapter1.Update(MyDataSet);
     MessageBox.Show("成功更新数据库记录:"+Rows.ToString()+"条!","信息提示",
      MessageBoxButtons.OK,MessageBoxIcon.Information);
     MyDataSet.AcceptChanges();    
    }
    else
    {
     MessageBox.Show("数据库记录没有变化","信息提示",
      MessageBoxButtons.OK,MessageBoxIcon.Information);    
    }
   }
   catch(Exception Err)
   {
    MessageBox.Show("更新数据库记录操作失败:"+Err.Message,"信息提示",
     MessageBoxButtons.OK,MessageBoxIcon.Information);
    //如果打开了连接,则关闭它
    if(this.sqlConnection1.State==ConnectionState.Open)
    {
     this.sqlConnection1.Close();
    }
   }      
  }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!要使用Python将Excel表格中的数据更新SQL Server数据库表中,可以按照以下步骤进行: 1. 安装必要的库 使用Python连接SQL Server需要用到 `pyodbc` 库,可以使用 `pip` 命令进行安装: ``` pip install pyodbc ``` 同时,如果需要读取和写入 Excel 文件,还需要安装 `pandas` 库: ``` pip install pandas ``` 2. 连接到 SQL Server 数据库 使用 `pyodbc` 库连接到 SQL Server 数据库,需要指定数据库的连接信息,例如: ```python import pyodbc server = 'server_name' # 数据库服务器名称 database = 'database_name' # 数据库名称 username = 'username' # 登录用户名 password = 'password' # 登录密码 # 使用 pyodbc 连接数据库 cnxn = pyodbc.connect(f'DRIVER={{SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password}') ``` 这样就可以使用 `cnxn` 变量来进行数据库操作。 3. 读取 Excel 数据 使用 `pandas` 库读取 Excel 文件数据,例如: ```python import pandas as pd # 读取 Excel 文件 df = pd.read_excel('file.xlsx', sheet_name='Sheet1') ``` 其中 `file.xlsx` 是 Excel 文件名,`Sheet1` 是要读取的工作表名称。 4. 更新 SQL Server 数据库表 使用 `pyodbc` 库执行 SQL 语句来更新数据库表,例如: ```python # 获取数据库游标 cursor = cnxn.cursor() # 更新数据库表 for row in df.itertuples(index=False): cursor.execute(f"UPDATE table_name SET column1='{row.column1}', column2='{row.column2}' WHERE id={row.id}") # 提交事务 cnxn.commit() # 关闭游标和连接 cursor.close() cnxn.close() ``` 其中 `table_name` 是要更新数据库表名,`column1` 和 `column2` 是要更新的列名,`id` 是用来唯一标识每一行数据的列名。这使用了 `for` 循环来遍历 Excel 中的每一行数据,并将其更新数据库表中。 最后记得提交事务并关闭游标和连接。 以上就是使用 Python 将 Excel 表格中的数据更新SQL Server 数据库表中的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值