SQLDataReader读取函数不返回任何内容 [英] SQLDataReader read function not returning anything
本文介绍了SQLDataReader读取函数不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我们有问题SQLDataReader的读取功能没有返回anuthing。请帮我弄清问题。
我从这个函数传递数据,顺便说一句是deleteEntry类
Hello guys I am having a problem the read function of SQLDataReader is not returning anuthing. Please help me to figure out the problem.
I am passing data from this function which by the way is of deleteEntry class
string name = this.textBox1.Text;
string connstring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Gardezi\Documents\Visual Studio 2012\Projects\homeWork2\homeWork2\Database1.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(connstring);
string query = "Select * from diaryDB where Title=@name";
SqlCommand com = new SqlCommand(query, con);
SqlParameter p = new SqlParameter("name", name);
com.Parameters.Add(p);
con.Open();
SqlDataReader d = com.ExecuteReader();
deleteResult r = new deleteResult(d);
con.Close();
r.Show();
然后在类deleteResult的构造函数中我正在初始化组合框。
Then here in constructor of class deleteResult I am intializing the combo box.
public deleteResult(SqlDataReader d)
{
InitializeComponent();
while (d.Read())
{
this.comboBox1.Items.Add((d["title"] + "-" + d["description"]).ToString());
}
}
但是当我尝试从数据库中获取数据时,它成功获取数据但是程序永远不会进入我试图读取数据的循环
But when I try to get data from the DB it successfully aquires the data but the program never enters the loop where I am trying to read the data
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string name = this.comboBox1.SelectedItem.ToString();
string[] arr = name.Split('-');
string p1 = arr[1];
string connstring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Gardezi\Documents\Visual Studio 2012\Projects\homeWork2\homeWork2\Database1.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(connstring);
string query = "Select * from diaryDB where Description=@p1";
SqlCommand com = new SqlCommand(query, con);
SqlParameter p = new SqlParameter("p1", name);
com.Parameters.Add(p);
con.Open();
SqlDataReader d = com.ExecuteReader();
int p11 = d.FieldCount;
MessageBox.Show(""+p11);
while (d.Read())
{
this.textBox1.Text = d["Title"].ToString();
this.richTextBox1.Text = d["Description"].ToString();
this.pictureBox1.Image = Image.FromFile(d["pic"].ToString());
}
con.Close();
query = "delete from diaryDB where Description=@p1";
com = new SqlCommand(query, con);
p = new SqlParameter("p1", p1);
com.Parameters.Add(p);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
推荐答案
SqlParameter p = new SqlParameter(p1,name);
- 应该名称
是p1
?
如果我不清楚...
SqlParameter p = new SqlParameter(p1,
name 罢工> p1 );
SqlParameter p = new SqlParameter("p1", name);
-- shouldname
bep1
?
In case I was unclear...
SqlParameter p = new SqlParameter("p1",
namep1);
这篇关于SQLDataReader读取函数不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文