-
大小: 49.96 KB文件類型: .rar金幣: 1下載: 1 次發(fā)布日期: 2024-11-27
- 語言: C#
- 標(biāo)簽: dataset??事務(wù)??數(shù)據(jù)插入??
資源簡介
一個源碼,包括10個例,說明數(shù)據(jù)表的合并,數(shù)據(jù)的不重復(fù)插入,以事務(wù)更新多個數(shù)據(jù)表以批量數(shù)據(jù)導(dǎo)入導(dǎo)出數(shù)據(jù)庫
大大有益啊。。。。
大大有益啊。。。。
代碼片段和文件信息
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(“該記錄已經(jīng)存在!“?“信息提示“?MessageBoxButtons.OK?MessageBoxIcon.Information);???????????????????
????????}
????????private?void?button2_Click(object?sender?EventArgs?e)
????????{//以參數(shù)化方式增加數(shù)據(jù)庫記錄
???????????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個文件信息
評論
共有 條評論