使用C#将记录添加或插入到SQL 2008中的多个表中 [英] Add or insert records to multiple tables in SQL 2008 using C #

查看:317
本文介绍了使用C#将记录添加或插入到SQL 2008中的多个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

请为我提供有关如何使用c sharp在sql 2008中向多个表中插入或添加记录的建议.

表1是员工
表2是BIRTHDAY

记录已完美添加到table1,我希望将其同时添加到table2.
以下是我正在使用的代码

Hi all,

Please advice me on how to insert or add records to multiple tables in sql 2008 using c sharp.

Table1 is EMPLOYEES
Table2 is BIRTHDAY

The record gets added to table1 perfectly, I want it to be added to table2 simultaneously.
below is the code which I am using

private void btnAdd_Click(object sender, EventArgs e)
        {
            SqlConnection DB_Connection = SQLConnect.Instance;
            SqlDataAdapter da = new SqlDataAdapter();            
            da.InsertCommand = new SqlCommand("INSERT INTO EMPLOYEES VALUES(@EMPID, @NAME, @DOB)", DB_Connection);
            
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("Please Fill in the Employee ID");
                textBox1.Focus();
                da.InsertCommand.Parameters.Add("@EMPID", SqlDbType.NVarChar).Value = DBNull.Value;
            }
            else
            {
                da.InsertCommand.Parameters.Add("@EMPID", SqlDbType.NVarChar).Value = textBox1.Text;
            }
           
           if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("Please Fill in the Employee Name");
                textBox2.Focus();
                DB_Connection.Close();
                //da.InsertCommand.Parameters.Add("@NAME", SqlDbType.NVarChar).Value = DBNull.Value;                
            }
            else
            {
                da.InsertCommand.Parameters.Add("@NAME", SqlDbType.NVarChar).Value = textBox2.Text;
            }
		
	    if (string.IsNullOrEmpty(dateTimePicker1.Text))
            {
                da.InsertCommand.Parameters.Add("@DOB", SqlDbType.Int).Value = DBNull.Value;
            }
            else
            {
                da.InsertCommand.Parameters.Add("@DOB", SqlDbType.Date).Value = dateTimePicker1.Value;
            }
System.Windows.Forms.TextBox sj = new TextBox();
            try
            {
                if (sj.Text.Length >= 0)
                {
                    MessageBox.Show("yes proceed");
                    DB_Connection.Open();
                    int ins = da.InsertCommand.ExecuteNonQuery();                    
                    MessageBox.Show(ins.ToString() + " Record Inserted to DB");
                    ClearForm(this);                    
                    DB_Connection.Close();
                }
                else
                {
                    MessageBox.Show("Fill in all the Details");
                }
            }
            catch (Exception yu)
            {
                MessageBox.Show(yu.Message);
            }

推荐答案

亲爱的朋友

如果两个操作需要同时完成,那么我建议您使用存储过程
将值作为参数传递并执行


da.InsertCommand =新的SqlCommand("EmployeeInfoInsert(@EMPID,@NAME,@DOB)",DB_Connection);
:
:
:
Dear Friend

If the two operations need to be completed simultaniously then i would suggest you to use stored procedures
pass values as argument and execute it


da.InsertCommand = new SqlCommand("EmployeeInfoInsert(@EMPID, @NAME, @DOB)", DB_Connection);
:
:
:
-------Stored Procedure ---------

createprocedure EmployeeInfoInsert(@EMPID varchar(20), @NAME varchar(20), @DOB datetime)
as
begin
   insert statement to EMPLOYEE table
   insert satatement to BIRTHDAY table 
end


这篇关于使用C#将记录添加或插入到SQL 2008中的多个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆