2017-09-19 176 views
1

從excel插入數據到數據庫表時出現錯誤。 這是錯誤附近有語法錯誤「NAME」 這是我的代碼:從excel插入數據到sql表

protected void btninsert_Click(object sender, EventArgs e) 
{ 
    foreach (GridViewRow g1 in GridView1.Rows) 
    { 
     conStr = ConfigurationManager.ConnectionStrings["SqlConString"].ConnectionString; 
     SqlConnection con = new SqlConnection(conStr); 

     SqlCommand com = new SqlCommand("insert into MedicalItems (ITEM NAME,GROUP,ITEM TYPE,COST PRICE,SELLING PRICE,PURCHASE UOM,PURCHASE PACKAGING,DISPENSING UOM,QTY ON HAND,EXPIRY DATE,REORDER LEVEL,REORDER QUANTITY,BATCH#) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "','" + g1.Cells[7].Text + "','" + g1.Cells[8].Text + "','" + g1.Cells[9].Text + "','" + g1.Cells[10].Text + "','" + g1.Cells[11].Text + "','" + g1.Cells[12].Text + "','" + g1.Cells[13].Text + "')", con); 

     con.Open(); 

     com.ExecuteNonQuery(); 

     con.Close(); 



    } 

    Label2.Text = "Records inserted successfully"; 

} 
+2

'ITEM NAME'。或者,有一個冒號丟失或者它應該是'[Item Name]'。 – waka

+0

[Little Bobby Tables](http://bobby-tables.com/)也對此代碼感到興奮。使用參數! – waka

回答

1

裹在[]像[項目名稱]的列名,爲所有空格列。