⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 数据库编程.txt

📁 此代码为在.net环境下 用c#进行数据库编程的核心功能代码 希望对网友们又帮助
💻 TXT
📖 第 1 页 / 共 2 页
字号:
			this.label3.Text = "电话";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.Add(this.button4);
			this.groupBox4.Controls.Add(this.button3);
			this.groupBox4.Controls.Add(this.button2);
			this.groupBox4.Controls.Add(this.button1);
			this.groupBox4.Location = new System.Drawing.Point(8, 192);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(472, 48);
			this.groupBox4.TabIndex = 5;
			this.groupBox4.TabStop = false;
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(352, 16);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(72, 24);
			this.button4.TabIndex = 3;
			this.button4.Text = "更新";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(240, 16);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(88, 24);
			this.button3.TabIndex = 2;
			this.button3.Text = "删除";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(136, 16);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(80, 24);
			this.button2.TabIndex = 1;
			this.button2.Text = "保存";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(40, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(72, 24);
			this.button1.TabIndex = 0;
			this.button1.Text = "插入";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// groupBox5
			// 
			this.groupBox5.ContextMenu = this.contextMenu1;
			this.groupBox5.Controls.Add(this.dataGrid1);
			this.groupBox5.ImeMode = System.Windows.Forms.ImeMode.On;
			this.groupBox5.Location = new System.Drawing.Point(16, 256);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(464, 216);
			this.groupBox5.TabIndex = 6;
			this.groupBox5.TabStop = false;
			// 
			// dataGrid1
			// 
			this.dataGrid1.AllowSorting = false;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.DataSource = this.dataSet11.BankCustomers;
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(8, 17);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(448, 192);
			this.dataGrid1.TabIndex = 0;
			this.dataGrid1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_mouseDown);
			// 
			// sqlConnection1
			// 
			this.sqlConnection1.ConnectionString = "workstation id=open064;packet size=4096;integrated security=SSPI;data source=open" +
				"064;persist security info=False;initial catalog=databaseTest";
			// 
			// sqlDataAdapter1
			// 
			this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
			this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
			this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
			this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																									  new System.Data.Common.DataTableMapping("Table", "BankCustomers", new System.Data.Common.DataColumnMapping[] {
																																																					   new System.Data.Common.DataColumnMapping("CustomerName", "CustomerName"),
																																																					   new System.Data.Common.DataColumnMapping("CustomerAddress", "CustomerAddress"),
																																																					   new System.Data.Common.DataColumnMapping("CustomerPhone", "CustomerPhone"),
																																																					   new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),
																																																					   new System.Data.Common.DataColumnMapping("Password", "Password")})});
			this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
			// 
			// sqlDeleteCommand1
			// 
			this.sqlDeleteCommand1.CommandText = @"DELETE FROM BankCustomers WHERE (CustomerID = @Original_CustomerID) AND (CustomerAddress = @Original_CustomerAddress OR @Original_CustomerAddress IS NULL AND CustomerAddress IS NULL) AND (CustomerName = @Original_CustomerName) AND (CustomerPhone = @Original_CustomerPhone OR @Original_CustomerPhone IS NULL AND CustomerPhone IS NULL) AND (Password = @Original_Password OR @Original_Password IS NULL AND Password IS NULL)";
			this.sqlDeleteCommand1.Connection = this.sqlConnection1;
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerAddress", System.Data.SqlDbType.VarChar, 80, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerAddress", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerName", System.Data.SqlDbType.VarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerName", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerPhone", System.Data.SqlDbType.Decimal, 9, System.Data.ParameterDirection.Input, false, ((System.Byte)(18)), ((System.Byte)(0)), "CustomerPhone", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Password", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Password", System.Data.DataRowVersion.Original, null));
			// 
			// sqlInsertCommand1
			// 
			this.sqlInsertCommand1.CommandText = @"INSERT INTO BankCustomers(CustomerName, CustomerAddress, CustomerPhone, Password) VALUES (@CustomerName, @CustomerAddress, @CustomerPhone, @Password); SELECT CustomerName, CustomerAddress, CustomerPhone, CustomerID, Password FROM BankCustomers WHERE (CustomerID = @@IDENTITY)";
			this.sqlInsertCommand1.Connection = this.sqlConnection1;
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerName", System.Data.SqlDbType.VarChar, 40, "CustomerName"));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerAddress", System.Data.SqlDbType.VarChar, 80, "CustomerAddress"));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerPhone", System.Data.SqlDbType.Decimal, 9, System.Data.ParameterDirection.Input, false, ((System.Byte)(18)), ((System.Byte)(0)), "CustomerPhone", System.Data.DataRowVersion.Current, null));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.NVarChar, 50, "Password"));
			// 
			// sqlSelectCommand1
			// 
			this.sqlSelectCommand1.CommandText = "SELECT CustomerName, CustomerAddress, CustomerPhone, CustomerID, Password FROM Ba" +
				"nkCustomers";
			this.sqlSelectCommand1.Connection = this.sqlConnection1;
			// 
			// sqlUpdateCommand1
			// 
			this.sqlUpdateCommand1.CommandText = @"UPDATE BankCustomers SET CustomerName = @CustomerName, CustomerAddress = @CustomerAddress, CustomerPhone = @CustomerPhone, Password = @Password WHERE (CustomerID = @Original_CustomerID) AND (CustomerAddress = @Original_CustomerAddress OR @Original_CustomerAddress IS NULL AND CustomerAddress IS NULL) AND (CustomerName = @Original_CustomerName) AND (CustomerPhone = @Original_CustomerPhone OR @Original_CustomerPhone IS NULL AND CustomerPhone IS NULL) AND (Password = @Original_Password OR @Original_Password IS NULL AND Password IS NULL); SELECT CustomerName, CustomerAddress, CustomerPhone, CustomerID, Password FROM BankCustomers WHERE (CustomerID = @CustomerID)";
			this.sqlUpdateCommand1.Connection = this.sqlConnection1;
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerName", System.Data.SqlDbType.VarChar, 40, "CustomerName"));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerAddress", System.Data.SqlDbType.VarChar, 80, "CustomerAddress"));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerPhone", System.Data.SqlDbType.Decimal, 9, System.Data.ParameterDirection.Input, false, ((System.Byte)(18)), ((System.Byte)(0)), "CustomerPhone", System.Data.DataRowVersion.Current, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.NVarChar, 50, "Password"));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerAddress", System.Data.SqlDbType.VarChar, 80, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerAddress", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerName", System.Data.SqlDbType.VarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerName", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerPhone", System.Data.SqlDbType.Decimal, 9, System.Data.ParameterDirection.Input, false, ((System.Byte)(18)), ((System.Byte)(0)), "CustomerPhone", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Password", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Password", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.Int, 4, "CustomerID"));
			// 
			// contextMenu1
			// 
			this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuItem2,
																						 this.menuItem4});
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 0;
			this.menuItem2.Text = "保存";
			this.menuItem2.Click += new System.EventHandler(this.button2_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 1;
			this.menuItem4.Text = "更新";
			this.menuItem4.Click += new System.EventHandler(this.button4_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(496, 486);
			this.Controls.Add(this.groupBox5);
			this.Controls.Add(this.groupBox4);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.groupBox5.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void dataGrid1_mouseDown(object sender, System.Windows.Forms.MouseEventArgs ne)
		{		
			int i=this.dataGrid1.HitTest(ne.X,ne.Y).Row;
			this.BindingContext[dataSet11,"BankCustomers"].Position=i;
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			this.sqlDataAdapter1.Fill(dataSet11);
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			int i=this.dataGrid1.CurrentCell.RowNumber;
			System.Console.WriteLine("i="+i);
			if(i<0)
			{
				MessageBox.Show("请选择要删除的行");
				return;
			}
			
			this.dataSet11.Tables["BankCustomers"].Rows[i].Delete();
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.sqlDataAdapter1.Update(this.dataSet11);
			//查询this.sqlDataAdapter1.SelectCommand="select id from...";
			this.sqlDataAdapter1.Fill(dataSet11);

			//可修改的查询System.Data.SqlClient.SqlCommand c=new System.Data.SqlClient.SqlCommand("",this.sqlConnection1);
c.ExecuteNonQuery();
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			DataRow newRow=this.dataSet11.Tables["BankCustomers"].NewRow();
			newRow["CustomerName"]="";
			this.dataSet11.Tables["BankCustomers"].Rows.Add(newRow);	
			this.BindingContext[dataSet11,"BankCustomers"].Position=this.dataSet11.Tables["BankCustomers"].Rows.Count-1;		
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -