0

以下代码可以在一行表中插入新值。但是当我尝试插入多个值时,在输出中这些值会在表中保存两次。

请帮助我获取可以在我的表中插入多个值的代码。

private void Form1_Load(object sender, EventArgs e)
 {
 String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

 SqlConnection con = new SqlConnection(strConnection);
 try
 {

 con.Open();

 SqlCommand sqlCmd = new SqlCommand();

 sqlCmd.Connection = con;
 sqlCmd.CommandType = CommandType.Text;
 sqlCmd.CommandText = "Select table_name from information_schema.tables";

 SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

 DataTable dtRecord = new DataTable();
 sqlDataAdap.Fill(dtRecord);
 comboBox1.DataSource = dtRecord;
 comboBox1.DisplayMember = "TABLE_NAME";
 comboBox1.ValueMember = "TABLE_NAME";

 con.Close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
 }
 private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
 {
 var collection = this.dataGridView1.Rows;
 string output = "";
 foreach (DataGridViewRow row in collection)
 {
 foreach (DataGridViewCell cell in row.Cells)
 {
 if (cell.Value != null)
 {
 output += cell.Value.ToString() + " ";
 this.Text = output;
 }
 }
 }
 }
 private void PopulateGridView(string tablename)

 {

 if (tablename == "System.Data.DataRowView")
 return;
 String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

 SqlConnection con = new SqlConnection(strConnection);
 try
 {

 con.Open();

 SqlCommand sqlCmd = new SqlCommand();

 sqlCmd.Connection = con;
 sqlCmd.CommandType = CommandType.Text;
 sqlCmd.CommandText = "Select * from " + tablename;

 SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

 DataTable dtRecord = new DataTable();
 sqlDataAdap.Fill(dtRecord);
 dataGridView1.DataSource = dtRecord;

 con.Close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
 }

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {

 if (comboBox1.SelectedValue != null)
 {
 PopulateGridView(comboBox1.SelectedValue.ToString());
 }
 }
 private void InsertInfo()
 {
 string connectionString = null;
 SqlConnection connection;
 SqlDataAdapter adapter = new SqlDataAdapter();

 connectionString = @"Data Source=HP\SQLEXPRESS;database=MK;Integrated Security=true";
 connection = new SqlConnection(connectionString);

        foreach (int rowIndex in lstNewRows)
        {


            string insrtQry = "insert into " + comboBox1.Text + " values(";

            foreach (DataGridViewCell cell in dataGridView1.Rows[rowIndex].Cells)
            {
                insrtQry += "'" + cell.Value.ToString() + "',";
            }

            insrtQry = insrtQry.TrimEnd(",".ToCharArray());

            insrtQry += ")";


 try
 {
 connection.Open();
 adapter.InsertCommand = new SqlCommand(insrtQry, connection);
 adapter.InsertCommand.ExecuteNonQuery();



 MessageBox.Show("Row inserted !! ");
 connection.Close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.ToString());
 }
 }
 }





 private void insert_Click(object sender, EventArgs e)
 {
 InsertInfo();
 }

 private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
 {
 lstNewRows.Add(e.Row.Index);
 }


 }
 }
4

2 回答 2

1

尝试改变:

string insrtQry = "insert into " + comboBox1.Text + " values(";

至:

string insrtQry = "INSERT INTO table_name (field1, field2, field3) VALUES (@value1, @Value2, @value3)"

还可以通过点击此处的链接在 MSDN.Microsoft.com 上查看 SQL 插入语句

祝你的代码好运!

更新:

好吧,我开始明白你在做什么。您可以尝试将组合框变成一个变量,例如:

if (comboBox1.Text  == "table1")
{
string insrtQry = "INSERT INTO table_name (field1, field2, field3) VALUES 
(@value1, @Value2, @value3)" 
} 
else
{ 

 string insrtQry = "INSERT INTO table_name2 (field1-2, field2-3, field3-4) VALUES 
 (values1, values2, values3)" 
}
于 2013-04-01T06:56:02.140 回答
0

我更喜欢表值类型并将其传递给存储过程 [或内联查询,尽管我不喜欢它] 检查此链接以获取详细信息

http://www.christian-etter.de/?tag=sqldbtype-structured

于 2013-04-01T07:00:08.947 回答