C# 在表中插入多个值?
以下代码可以在一行表中插入新值。但当我尝试插入多个值时,这些值在输出中会在表中保存两次 请帮助我获取在我的表中插入多个值的代码C# 在表中插入多个值?,c#,sql,vb.net,winforms,visual-studio-2010,C#,Sql,Vb.net,Winforms,Visual Studio 2010,以下代码可以在一行表中插入新值。但当我尝试插入多个值时,这些值在输出中会在表中保存两次 请帮助我获取在我的表中插入多个值的代码 private void Form1_Load(object sender, EventArgs e) { String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true"; SqlConnection con = new SqlConnection(
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);
}
}
}
尝试更改:
string insrtQry = "insert into " + comboBox1.Text + " values(";
致:
还可以通过以下链接在MSDN.Microsoft.com上签出SQL insert语句
祝你的代码好运
更新:
好了,我开始明白你在干什么了。您可以尝试将comboBox变成一个变量,如下所示:
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)"
}
我更喜欢表值类型,并将其传递给存储过程[或内联查询,尽管我不喜欢它] 查看此链接了解详细信息
顺便问一下,新行是从哪里来的?它是一个列表框吗?或者什么?我已经声明了这个列表lstNewRows=newlist();在代码的开头…我使用了一个组合框,你有没有试着在“InsertInfo()”的开头加上断点?如果这被调用了两次?不,我没有告诉你在哪里放置断点以便检查我不确定你是否需要为此创建一个
InsertCommand
,你只需将SQL查询输入到CommandText
,而不必设置InsertCommand
。不,先生,我无法替换这一行,因为我想从组合框中选择任何表,然后用值插入它。但是,先生,我的组合框中有两个不同的表…这意味着它们有不同的字段,因此请帮助如何替换这一代码行。是的,先生,非常感谢它的工作…但它在void of statements private void中显示错误insert_Click(object sender,EventArgs e)和private void dataGridView1_DefaultValuesNeed(object sender,DataGridViewRowEventArgs e)您是否为datdGridView分配了任何默认变量,或将其与数据库连接了?这是一个相当标准的错误,我建议您从更改void开始,“InsertInfo”一开始是公开的而不是私人的,看看这是否会改变eror
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)"
}