将超文本插入数据库
Inserting hypertext into database
你好,我正在尝试将超文本插入数据库 table.First 我将所有单词分开,使它们成为小写字母,并在 listbox1 上列出,这样可以正常工作。
这是我的 table 词;
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 requires an open and available Connection. The
connection's current state is closed.
编辑 1:
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();
}
我已经这样改变了,但现在我收到了这个错误
连接没有关闭。连接的当前状态是打开的。
尝试将您的最后几行代码更改为我的示例。每次迭代结果集合时,您都关闭了与数据库的连接。
using (conn){
conn.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 = new SqlCommand("insert into word values('" + id + "','" + item.Word.Text + "','0'),'0'),'0'),'0')", con);
cmd.ExecuteNonQuery();
}
}
您必须在
之后关闭连接
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 = new SqlCommand("insert into word values('" + id + "','" + item.Word.Text + "','0'),'0'),'0'),'0')", con);
cmd.ExecuteNonQuery();
}
}
con.close();
你好,我正在尝试将超文本插入数据库 table.First 我将所有单词分开,使它们成为小写字母,并在 listbox1 上列出,这样可以正常工作。
这是我的 table 词;
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 requires an open and available Connection. The connection's current state is closed.
编辑 1:
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();
}
我已经这样改变了,但现在我收到了这个错误
连接没有关闭。连接的当前状态是打开的。
尝试将您的最后几行代码更改为我的示例。每次迭代结果集合时,您都关闭了与数据库的连接。
using (conn){
conn.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 = new SqlCommand("insert into word values('" + id + "','" + item.Word.Text + "','0'),'0'),'0'),'0')", con);
cmd.ExecuteNonQuery();
}
}
您必须在
之后关闭连接 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 = new SqlCommand("insert into word values('" + id + "','" + item.Word.Text + "','0'),'0'),'0'),'0')", con);
cmd.ExecuteNonQuery();
}
}
con.close();