使用c#在表中插入多个值 [英] inserting multiple values in tables using c#

查看:106
本文介绍了使用c#在表中插入多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

the following code is working fine to insert new values in one row of tables...but when i try to insert multiple values then in the output those values get saved twice in the table...plz help

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);
 }


 }
 }

推荐答案

只是看起来很快 - 但是你不是用表中的行填充网格 - 然后迭代那些行并插入它们?所以你要插入已经存在的所有记录?所以它不是两次插入它们,它只是再次插入现有的行?
Only looked quickly - but aren''t you populating the grid with the rows from the table - then iterating that collection of rows and inserting them? So you''re inserting all the records that are already there? So it''s not inserting them twice, it''s just inserting existing rows again?


这篇关于使用c#在表中插入多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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