private void Save_Click(object sender, EventArgs e)
{
string strconn = @"Server=.\SQLEXPRESS;initial catalog=PharmacyV2;integrated security=true;";
SqlConnection conn = new SqlConnection(strconn);
//SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Units",conn);
da.Fill(ds, "Units");
bool found = false;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < ds.Tables["Units"].Rows.Count; j++)
{
if (ds.Tables["Units"].Rows[j][0].ToString() == dataGridView1.Rows[i].Cells[0].Value.ToString())
{
found = true;
break;
}
}
if (found==false)
{
SqlCommand cmd;
cmd = new SqlCommand("insert into Units (Unit_name) values (@name)", conn);
cmd.Parameters.AddWithValue("@name", dataGridView1.Rows[i].Cells[0].Value.ToString());
cmd.ExecuteNonQuery();
MessageBox.Show("تمت الاضافه");
}
}
conn.Close();
}
내 프로그램이 데이터베이스 의 중복을 방지하기 위해 데이터베이스에서 UINT 테이블의 모든 요소에있는 DataGridView에서 각 요소를 비교 데이터베이스에 항목을 중복 statement 프로그램이 데이터베이스에 데이터를 삽입하지 않는 이유는 무엇입니까? 는예방
오류 또는 예외 메시지가 있습니까? –
'dataGridView1.Rows.Count'의 값은 무엇입니까? 중단 점을 넣은 다음 확인하십시오 – sll
그리고 SqlConnection 객체를 삭제하십시오. 즉, (SqlConnection conn = new SqlConnection (strconn)) {0125.BuildByyyyyyyy} – Stefan