本文介绍了通过从组合框中选择它们,在不同的表中插入新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码..它没有显示任何错误,但它的nt working..i意味着没有插入新值
private void PopulateComboBox()
{
try
{
List< string> _items = new List< string>();
_items.Add(select * from lol);
_items.Add(select * from datejoin);
comboBox1.DataSource = _items;
}
catch(exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender,EventArgs e)
{
PopulateComboBox();
}
private void PopulateGridView(string connString,string sqlQuery)
{
String strconnection = connString;
SqlConnection con = new SqlConnection(strconnection);
尝试
{
con.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = sqlQuery;
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
DataTable dtRecord = new DataTable();
sqlDataAdap.Fill(dtRecord);
dataGridView1.DataSource = dtRecord;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
if(comboBox1.SelectedValue!= null)
{
PopulateGridView(textBox1.Text,comboBox1.SelectedValue.ToString());
}
}
private void InsertInfo()
{
string connectionString = null;
SqlConnection连接;
SqlDataAdapter adapter = new SqlDataAdapter();
connectionString = @Data Source = HP\SQLEXPRESS; database = MK; Integrated Security = true;
connection = new SqlConnection(connectionString);
foreach(lstNewRows中的int rowIndex)
{
string insrtQry =insert into+ comboBox1.SelectedText +values(;
foreach(dataGridView1.Rows [rowIndex] .Cells中的DataGridViewCell单元格)
{
insrtQry + =''+ cell.Value.ToString()+'',;
}
insrtQry = insrtQry.TrimEnd(,。ToCharArray());
insrtQry + =);
尝试
{
connection.Open();
adapter.InsertCommand = new SqlCommand(insrtQry,connection);
adapter.InsertCommand.ExecuteNonQuery();
MessageBox.Show(Row inserted !!);
}
catch(exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
private void button1_Click(object sender,EventArgs e)
{
InsertInfo();
}
private void dataGridView1_DefaultValuesNeeded(object sender,DataGridViewRowEventArgs e)
{
lstNewRows.Add(e.Row.Index);
}
}
}
解决方案
i hv used the following code..it is not showing any error but its nt working..i mean new values r not being inserted
private void PopulateComboBox() { try { List<string> _items = new List<string>(); _items.Add("select * from lol"); _items.Add("select * from datejoin"); comboBox1.DataSource = _items; } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void Form1_Load(object sender, EventArgs e) { PopulateComboBox(); } private void PopulateGridView(string connString, string sqlQuery) { String strconnection = connString; SqlConnection con = new SqlConnection(strconnection); try { con.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = con; sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = sqlQuery; SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd); DataTable dtRecord = new DataTable(); sqlDataAdap.Fill(dtRecord); dataGridView1.DataSource = dtRecord; dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedValue != null) { PopulateGridView(textBox1.Text,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.SelectedText + " 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 !! "); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } private void button1_Click(object sender, EventArgs e) { InsertInfo(); } private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) { lstNewRows.Add(e.Row.Index); } } }
解决方案
这篇关于通过从组合框中选择它们,在不同的表中插入新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!