• 大小: 49.96 KB
    文件类型: .rar
    金币: 1
    下载: 1 次
    发布日期: 2024-11-27
  • 语言: C#
  • 标签: dataset  事务  数据插入  

资源简介

一个源码,包括10个例,说明数据表的合并,数据的不重复插入,以事务更新多个数据表以批量数据导入导出数据库
大大有益啊。。。。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using  System.Transactions;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender EventArgs e)
        {//避免增加除主键外的相同记录
           string MyCompanyName=“深圳唯佳物流公司“;
           string MyPhone=“0589-86523158“;
           string MySQL=“SELECT * FROM Shippers“;
           string MyConnectionString=“Data Source = .;Database =  Northwind;Integrated Security=SSPI“;           
           SqlDataAdapter  MyAdapter=new SqlDataAdapter(MySQLMyConnectionString);
           SqlCommandBuilder MyBuilder=new SqlCommandBuilder(MyAdapter);
           DataSet MySet=new DataSet();
           MyAdapter.Fill(MySet “Shippers“);
           DataColumn []MyKeys=new DataColumn[2];
           MyKeys[0] = MySet.Tables[“Shippers“].Columns[“CompanyName“];
           MyKeys[1] = MySet.Tables[“Shippers“].Columns[“Phone“];
           MySet.Tables[“Shippers“].PrimaryKey = MyKeys;
           string[] MySupplier = {MyCompanyName MyPhone};
           DataRow MyFindRow = MySet.Tables[“Shippers“].Rows.Find(MySupplier);
           if (MyFindRow == null)
           {
               DataRow MyNewRow =MySet.Tables[“Shippers“].NewRow();
               MyNewRow[“CompanyName“] = MySupplier[0];
               MyNewRow[“Phone“] = MySupplier[1];
               MySet.Tables[“Shippers“].Rows.Add(MyNewRow);
               MyAdapter.Update(MySet “Shippers“);
               MessageBox.Show(“增加记录操作成功!“ “信息提示“ MessageBoxButtons.OK MessageBoxIcon.Information);
           }
           else
               MessageBox.Show(“该记录已经存在!“ “信息提示“ MessageBoxButtons.OK MessageBoxIcon.Information);                   
        }

        private void button2_Click(object sender EventArgs e)
        {//以参数化方式增加数据库记录
           string MyCompanyName=“深圳唯佳物流公司“;
           string MyPhone=“0589-86523158“;
           string MyConnectionString=“Data Source = .;Database =  Northwind;Integrated Security=SSPI“;           
           SqlConnection MyConnection=null;
           try
           {
             MyConnection=new SqlConnection(MyConnectionString);
             MyConnection.Open();
             SqlCommand MyCommand=MyConnection.CreateCommand();
             MyCommand.CommandText = “INSERT INTO Shippers VALUES(@CompanyName @Phone)“;
             MyCommand.Parameters.Add(new SqlParameter(“@CompanyName“ SqlDbType.VarChar30));
             MyCommand.Parameters.Add(new SqlParameter(“@Phone“ SqlDbType.VarChar 30));
             MyCommand.Prepare();
             MyCommand.Parameters[“@CompanyName“].Value =MyCompanyName;
             MyCommand.Parameters[“@Phone“].Value =MyPhone;
             MyCommand.Execut

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        946  2006-05-30 15:27  N019\WindowsApplication1.sln

    ..A..H.     14848  2006-11-30 13:40  N019\WindowsApplication1.suo

     文件       8971  2006-08-14 00:32  N019\WindowsApplication1\Form1.Designer.cs

     文件      20074  2006-08-14 00:44  N019\WindowsApplication1\Form1.cs

     文件       5814  2006-08-14 00:32  N019\WindowsApplication1\Form1.resx

     文件        478  2006-05-30 15:27  N019\WindowsApplication1\Program.cs

     文件       3294  2006-07-25 22:09  N019\WindowsApplication1\WindowsApplication1.csproj

     文件       1210  2006-05-30 15:27  N019\WindowsApplication1\Properties\AssemblyInfo.cs

     文件       2892  2006-05-30 15:27  N019\WindowsApplication1\Properties\Resources.Designer.cs

     文件       5612  2006-05-30 15:27  N019\WindowsApplication1\Properties\Resources.resx

     文件       1102  2006-05-30 15:27  N019\WindowsApplication1\Properties\Settings.Designer.cs

     文件        249  2006-05-30 15:27  N019\WindowsApplication1\Properties\Settings.settings

     文件       5632  2005-11-11 22:25  N019\WindowsApplication1\bin\Debug\WindowsApplication1.vshost.exe

     文件      28672  2008-12-06 09:49  N019\WindowsApplication1\bin\Release\WindowsApplication1.exe

     文件      38400  2008-12-06 09:49  N019\WindowsApplication1\bin\Release\WindowsApplication1.pdb

     文件       5632  2005-11-11 22:25  N019\WindowsApplication1\bin\Release\WindowsApplication1.vshost.exe

     文件        368  2006-11-30 13:29  N019\WindowsApplication1\obj\WindowsApplication1.csproj.FileList.txt

     文件        180  2008-12-06 09:49  N019\WindowsApplication1\obj\Release\WindowsApplication1.Form1.resources

     文件        180  2008-12-06 09:49  N019\WindowsApplication1\obj\Release\WindowsApplication1.Properties.Resources.resources

     文件      28672  2008-12-06 09:49  N019\WindowsApplication1\obj\Release\WindowsApplication1.exe

     文件      38400  2008-12-06 09:49  N019\WindowsApplication1\obj\Release\WindowsApplication1.pdb

     文件        842  2008-12-06 09:49  N019\WindowsApplication1\obj\Release\WindowsApplication1.csproj.GenerateResource.Cache

     文件        824  2008-12-06 09:51  N019\WindowsApplication1\obj\WindowsApplication1.csproj.FileListAbsolute.txt

    ..AD...         0  2007-11-23 13:28  N019\WindowsApplication1\obj\Debug\TempPE

    ..AD...         0  2007-11-23 13:28  N019\WindowsApplication1\obj\Release\TempPE

    ..AD...         0  2007-11-23 13:28  N019\WindowsApplication1\bin\Debug

    ..AD...         0  2007-11-23 13:28  N019\WindowsApplication1\bin\Release

    ..AD...         0  2007-11-23 13:28  N019\WindowsApplication1\obj\Debug

    ..AD...         0  2007-11-23 13:28  N019\WindowsApplication1\obj\Release

    ..AD...         0  2007-11-23 13:28  N019\WindowsApplication1\Properties

............此处省略7个文件信息

评论

共有 条评论