我想要下表的转换

时间:2016-12-28 09:28:08

标签: c# sql-server-2008

** CODE HERE`protected void BTRegPatient_Click(object sender,EventArgs e)     {         SqlCommand cmd1 = new SqlCommand();         cmd1.CommandText =“IDAUTOINCREMENT_PATIENT”;         int id = db.MAX_ID(cmd1);

    LabelActive.Text = 0.ToString();

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "INSERT_PATIENT";
    cmd.Parameters.AddWithValue("@ID", id);
    cmd.Parameters.AddWithValue("@FIRSTNAME", TxtFirstName.Text);
    cmd.Parameters.AddWithValue("@LASTNAME", TxtLastName.Text);
    cmd.Parameters.AddWithValue("@EMAILID", TxtEmailId.Text);
    cmd.Parameters.AddWithValue("@PASSWORD", TxtPassword.Text);
    cmd.Parameters.AddWithValue("@PHONENO", TxtPhoneNo.Text);
    cmd.Parameters.AddWithValue("@ACTIVE", LabelActive.Text);
    cmd.Parameters.AddWithValue("@TYPE", "Patient");
    db.NONRET(cmd);


    SqlCommand cmd2 = new SqlCommand();
    cmd2.CommandText = "IDAUTOINCREMENT_MAPPING";
    int id1 = db.MAX_ID(cmd2);

    string chkbox = string.Empty;

    for(int i=0;i<CBLPhysicianlist.Items.Count;i++)
    {
        if(CBLPhysicianlist.Items[i].Selected==true)
        {
            chkbox = chkbox + CBLPhysicianlist.Items[i].Value.ToString() + ",";

        }

    }
    chkbox = chkbox.TrimEnd(',');


    SqlCommand cmd4 = new SqlCommand();
    cmd4.CommandText = "INSERT_MAPPING";

    cmd4.Parameters.AddWithValue("@ID", id1);
    cmd4.Parameters.AddWithValue("@PATID", id);
    cmd4.Parameters.AddWithValue("@PHYID", chkbox);
    db.NONRET(cmd4);

    Response.Write("<script>alert('Success')</script>");

    TxtEmailId.Text = "";
    TxtFirstName.Text = "";
    TxtLastName.Text = "";
    TxtPassword.Text = "";
    TxtPhoneNo.Text = "";
    CBLPhysicianlist.ClearSelection();

}

}`**

上面的按钮代码注册点击。我想把数据传递到桌子上PHYSICIAN_PATIENT_MAPPING ..

1 个答案:

答案 0 :(得分:0)

问题的解决方案是查询数据库表,使用您选择的方法获取所有记录。

查询后,您可以删除所有记录。

对所有记录运行循环,并使用PHYSICIAN_ID字段上的string[]方法获取包含该记录的所有医生ID的var table = new DataTable(); // Create table for storing query results var connection = new SqlConnection("connectionString"); // Declare connection to server connection.Open(); // Attempt to connect to specified server var command = new SqlCommand("SELECT * FROM Table", connection); // Declare query for server var adapter = new SqlDataAdapter(command); // Declare a data adapter for the query adapter.Fill(table); // Execute the query var deleteCommand = new SqlCommand("DELETE FROM Table", connection); // Declare delete statement deleteCommand.ExecuteNonQuery(); // Execute delete statement var strBuilder = new StringBuilder(); // StringBuilder for creating SQL statements foreach (DataRow record in table.Rows) // For every row in the table { string[] physicians = record["PHYSICIAN_ID"].ToString().Split(','); // Turn the CSV format into a string[] foreach (string physician in physicians) // For all physicians for that patient { strBuilder.Append("INSERT INTO Table (PATIENT_ID, PHYSICIAN_ID) VALUES (" + record["PATIENT_ID"].ToString() + ", " + physician + ");"); // Append the StringBuilder with a statement to insert the new record // for every physician as applicable } } var insertCommand = new SqlCommand(strBuilder.ToString(), connection); // Create insert statements from the StringBuilder insertCommand.ExecuteNonQuery(); // Execute insert statements connection.Close(); // Close connection to the server

获得后,您可以为每个记录中的每个医生ID运行一个插入语句,以根据需要插入所有记录。

这是我在C#代码方面的意思的一个例子(由评论记录):

{{1}}