你好,我試圖插入一個超文本到數據庫表中。首先我分離所有的單詞,使他們的小寫字母和listbox1列表上的作品正確。將超文本插入到數據庫中
這是我的餐桌單詞;
id(int),word(nvarchar),sid(int),frequency(int),weight(float),f(boolean) by order
protected void Button1_Click(object sender, EventArgs e)
{
int id=0;
ListBox1.Items.Clear();
string strNew = Request.Form["TextBox1"];
// File.WriteAllText(@"\Users\'uykusuz\Documents\text.txt", strNew);
int n = strNew.Split(' ').Length;
strNew=strNew.ToLower();
var results = strNew.Split(' ').Where(x => x.Length > 1)
.GroupBy(x => x)
.Select(x => new { Count = x.Count(), Word = x.Key })
.OrderByDescending(x => x.Count);
foreach (var item in results)
ListBox1.Items.Add(String.Format("{0} occured {1} times", item.Word, item.Count));
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 = new SqlCommand("insert into word values('" + id + "','" + item.Word.Text + "','0'),'0'),'0'),'0')", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
當我按下按鈕時,總是出現此錯誤;
ExecuteNonQuery需要一個開放且可用的Connection。連接的當前狀態將關閉。
EDIT1:
foreach (var item in results) {
con.Open();
id++;
SqlCommand cmd = new SqlCommand("insert into word values('" + id + "','" + item.Word + "','0','0','0','0')", con);
cmd.ExecuteNonQuery();
con.Close();
}
我已經改變了這樣的,但現在我得到這個錯誤
連接沒有關閉。連接的當前狀態已打開。
cmd.ExecuteNonQuery前con.Open()()後的連接......你說你的查詢將使用的連接,但你沒有打開它 – Veljko89