다음 코드는 테이블의 한 행에 새 값을 삽입하는 데 적합합니다. 그러나 출력에 여러 값을 삽입하려고하면 해당 값이 테이블에 두 번 저장됩니다.테이블에 값을 여러 개 삽입 하시겠습니까?
내 테이블에 여러 값을 삽입하는 데 도움이되는 코드를 알려주십시오.
string insrtQry = "insert into " + comboBox1.Text + " values(";
에 :
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);
}
}
}
btw 여기서 lstNewRows는 (는) ListBox입니까? 또는 무엇을? –
이 목록을 선언했습니다. lstNewRows = new List (); 코드 시작 ... 그리고 메신저 콤보 상자 사용 –
"InsertInfo()"의 시작 부분에 중단 점을 넣으려고 했습니까? 이것이 두 번 어떻게 든 불려지면? –