开发者

Update but in button_click event

con.Open();
    SqlCommand cmd1 = new SqlCommand("update Department_Master  Dept_code='"+txtDepartment.Text+"'where  " ,con);
    SqlCommand cmd2 = new SqlCommand("update Designation_Master  'DE_1','DS_1','" + txtDesignation.Text + "'", con);
    SqlCommand cmd3 = new SqlCommand("update into Position_Master values('" + var1 + "','" + var2 + "',@n,'" + txtHietrchy.Text + "')", con);
    cmd3.Parameters.Add("@n", SqlDbType.Int).Value = n;
    cmd3.ExecuteNonQuery();
    m++;

    cmd1.ExecuteNonQuery();
    cmd1.ExecuteNonQuery();
    cmd2.ExecuteNonQuery();

    con.Close();

I want to update three tables based on primary key,each table have this primary key as forgein key. There are three tables

this is the primary key autogeneration code....

    txtDesignation.Enabled = true;
    con.Open();
    int[] a = new int[100];
    int i = 0;
    String st = "select  substring(Dept_code ,4,10)as DE_ID from Department_Master";
    SqlDataAdapter da = new SqlDataAdapter(st, con);
    DataSet ds = new DataSet();
    da.Fill(ds);
   开发者_如何学运维 DataTable dt = ds.Tables[0];
    if (ds.Tables[0].Rows.Count != 0)
    {
        foreach (DataRow dr in dt.Rows)
        {
            a[i] = Convert.ToInt32(dr["DE_ID"].ToString());
            i++;
        }
        int cv = a.Max();
        var1 = "DE_" + Convert.ToString(cv + 1);
    }
    else
    {
        var1 = "DE_1";
    }
    SqlCommand cmd1 = new SqlCommand("insert into Department_Master values('" + var1 + "', '" + txtDepartment.Text + "')", con);
    cmd1.ExecuteNonQuery();

    //<..department end.........>

    int[] b= new int[100];
    int j = 0;
    string st1 = "select substring(Dsgn_Code,4,10)as DS_ID from Designation_Master";
    SqlDataAdapter da1 = new SqlDataAdapter(st1,con);
    DataSet ds1 = new DataSet();
    da1.Fill(ds1);
    DataTable dt1 = ds1.Tables[0];
    if (ds1.Tables[0].Rows.Count != 0)
    {
        foreach (DataRow dr1 in dt1.Rows)
        {
            b[j] = Convert.ToInt32(dr1["DS_ID"].ToString());
            j++;
        }
        int cv1 = b.Max();
        var2 = "DS_" + Convert.ToString(cv1 + 1);
    }
    else 
    {
        var2 = "DS_1";
    }
    SqlCommand cmd2 = new SqlCommand("insert into Designation_Master values('"+var1+"','" + var2 + "', '" + txtDesignation.Text + "')", con);
    cmd2.ExecuteNonQuery();

    //<........designation end................>

    SqlCommand cmd3 = new SqlCommand("insert into Position_Master values('"+var1+"','"+var2+"',@m,'"+txtHietrchy.Text+"')", con);
    cmd3.Parameters.Add("@m", SqlDbType.Int).Value = m;
    cmd3.ExecuteNonQuery();
    m++;

    //<.....position end......>


While your question is not clear, I would like to post a few comments here.

The update queries look incomplete. They may result in SQL exception. Queries in cmd1 and cmd2 doesn't have set. First query contains 'where' with no constraints. 3rd Query appears like insert and you have placed update in it.

Above all, you are directly placing a textbox's content in your query. SQL INJECTION is very much possible here.

My first advice is review your queries. Try to work on them in SQL server and if it works there bring them to C#.

And then, please avoid such inline queries. They are dangerous. Use parameters and Stored procedures.

Edited

To use the primary key generated in one table in the next insert/update, use @@identity, or SCOPE_IDENTITY().

For example, in your case,

the first query would be like this.

INSERT INTO Department_Master (....) VALUES (....)

Let's say it returns a Identity as a primary key (department_id)

the second query should go like this.

INSERT INTO Designation_Master (..., department_id,...) Values (..., @@identity, ...)

Remember, they should all fall in the same scope/ same connection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜