我想插入一些單詞,我作爲一個項目保存。我的項目是asp.net web項目。我想爲每個單詞生成id並保留4個字段null.There數據庫中的字表中有6個字段.id字長fr布爾量。在Asp.net中插入項目集合
當我運行該項目時,我得到這個錯誤。
ExecuteNonQuery需要一個開放且可用的Connection。連接的當前狀態將關閉。
這裏的代碼,
using (con){
con.Open();
foreach (var item in results) {//here trying to insert word its id and some other informations but for now they can stay null(yes,null allowed for them)
id++;
SqlCommand cmd= con.CreateCommand();
cmd.CommandText= "insert into word values('"+id+"','"+item.Word+"','"+0+"','"+0+"','"+0+"','"+0+"')";
cmd.ExecuteNonQuery();
con.Close();
}
編輯:
我提出con.Close了迭代循環,但現在在這裏我得到這個錯誤;
'/'應用程序中的服務器錯誤。 's'附近語法不正確。在字符'''後面的字符 未使用的引號。
using (con){
con.Open();
foreach (var item in results) {//here trying to insert word its id and some other informations but for now they can stay null(yes,null allowed for them)
id++;
SqlCommand cmd= con.CreateCommand();
cmd.CommandText= "insert into word values('"+id+"','"+item.Word+"','"+0+"','"+0+"','"+0+"','"+0+"')";
cmd.ExecuteNonQuery();
} con.Close();
} }
您正在關閉for循環的每次迭代中的連接。 –
我已將它移到外面,它給了我這個錯誤「附近語法不正確」 字符串')'後面未加上引號。「 – user1953051
你可以更新你的問題,包括你的代碼,因爲它是現在? –