c#中数据库连接 返回的dataset读取问题
各位大虾,我是个c#新手,现在遇到一个问题,亟待解决:
我用c# 编写了一个应用程序,在连接到SQL serve数据库时遇到问题了.我连接到数据库,返回结果dataset,我为了选择结果中特定的某条纪录,进行string类型比较时发现不能比较,我快急死了.各位大虾帮帮忙啊!我的部分代码如下:
public void getconnection() //和数据库建立连接
{
string petnamee;
string petcodee;
string connectionString = "server=localhost; " +
"Trusted_Connection=yes; database=pigpet ";
// 从Customers表获取记录
string commandString = "Select petname,petcode from pigpet ";
// 创—ataSet命令对象
// 和DataSet
SqlDataAdapter DataAdapter = new SqlDataAdapter(commandString, connectionString);
DataSet Dataset = new DataSet();
// 填充DataSet对象
DataAdapter.Fill(Dataset, "pigpet ");
// 从DataSet获取一个表
DataTable datatable = Dataset.Tables[0];
// 对于表中的每一列,显示信息
datatable = Dataset.Tables[0];
int rows = datatable.Rows.Count;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
for (int i = 0; i < rows; i++)
{
petnamee = datatable.Rows[i][ "petname "].ToString();
petcodee = datatable.Rows[i][ "petcode "].ToString();
if (this.comboBox1.Text == petnamee & this.textBox1.Text == petcodee)
{
Form1 form1 = new Form1();
form1.Show();
this.Hide();
petkey =1;
}
}
if (petkey!= 1)
{
Form13 form13 = new Form13();
form13.Show();
this.Hide();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
经调试我发现能返回结果,是 if (this.comboBox1.Text == petnamee & this.textBox1.Text == petcodee)总不成立,即使返回的结果中有是它成立的条件
例如,我让this.comboBox1.Text= "mmm ",this.textBox1.Text= "nnn ",数据库中有相应的纪录,但if语句还是不执行.各位大虾麻烦该看看吧.帮帮我吧
[解决办法]
F5 一下 后F11
“// 从Customers表获取记录
string commandString = "Select petname,petcode from pigpet ";”
说是从Customers表获取记录 ,但是为什么是Select petname,petcode from pigpet这个
而不是Select petname,petcode from Customers
你的ADO.NET 要加强啊。。。
你可以到我的博客上坐坐。。。ado.net c#.net2005 From第一讲
[解决办法]
确认其他地方没错的话,if (this.comboBox1.Text == petnamee & this.textBox1.Text == petcodee)这里请再加一个&,即if (this.comboBox1.Text == petnamee && this.textBox1.Text == petcodee)
[解决办法]
& 这个符号 应该 写为 && 吧!
没用过 & 呢。
[解决办法]
try
if (this.comboBox1.Text == petnamee & this.textBox1.Text == petcodee)
改为:
if (this.comboBox1.Text == petnamee && this.textBox1.Text == petcodee)
[解决办法]
try 去掉空格
if (this.comboBox1.Text.Trim() == petnamee.Trim() && this.textBox1.Text.Trim() == petcodee.Trim())
[解决办法]
LZ 调试的时候
petnamee = datatable.Rows[i][ "petname "].ToString();
petcodee = datatable.Rows[i][ "petcode "].ToString();
这2个变量能取到值不?
[解决办法]
在 int rows = datatable.Rows.Count; 处设个断点,跟踪调试下
[解决办法]
是不是循环处理的不好?
今天晚上没环境测试,明天有时间再回帖...
[解决办法]
for (int i = 0; i < rows.count; i++)
//注意这里:应该是rows.count
{
petnamee = datatable.Rows[i][ "petname "].ToString();
petcodee = datatable.Rows[i][ "petcode "].ToString();
if (this.comboBox1.Text == petnamee & this.textBox1.Text == petcodee)
// 这里很显然&应为&&
{
Form1 form1 = new Form1();
form1.Show();
this.Hide();
petkey =1;
}
}
或者你最好用foreach
foreach(DataRow dr in datatable.Rows)
{
petnamee = dr[ "petname "].ToString();
petcodee = dr[ "petcode "].ToString();
// ...
}
------解决方案--------------------
对不起,看错了。
汗。。。
感冒了,注意力不集中。
[解决办法]
C# 中,当用于逻辑运算时, && 与 & , || 于 | 等价
[解决办法]
看不透,
打个断点,然后 debug,看看具体的值,
或者,输出到 DataGrid ,确认是否真的有目标值
且 for 循环内部似乎不合情理,假如找到目标值, 应该 break
[解决办法]
if (this.comboBox1.Text == petnamee & this.textBox1.Text == petcodee)
--->
if (this.comboBox1.Text == petnamee && this.textBox1.Text == petcodee)
[解决办法]
我在本地测试了一下,语句没有错误,你将this.comboBox1.Text,this.textBox1.Text附上具体值再试一下,是不是数据库中没有相应数据?
[解决办法]
if (this.comboBox1.Text == petnamee & this.textBox1.Text == petcodee)
改为:
if ((this.comboBox1.Text == petnamee) && (this.textBox1.Text == petcodee))
若还是不行,估计是你的combobox1与textbox1取出来的值有问题了,
[解决办法]
是 if (this.comboBox1.Text == petnamee & this.textBox1.Text == petcodee)总不成立,即使返回的结果中有是它成立的条件
=========================================
改为if(this.comboBox1.Text.ToString().equals(petnamee) & this.textBox1.Text.ToString().equals(petcodee))
=========================================
还有一个问题就是LZ的代码有问题,那个DataSet 实例的值为空。因为你的代码都没有去执行,所以,无论你怎么去取值,怎么去比较,都是没有用的...
[解决办法]
if (petnamee.StartsWith(this.comboBox1.Text.Trim()) && petcodee.StartsWith(this.textBox1.Text.Trim())
这样就防止了数据库取出来的数据有空格或者空字符.
[解决办法]
LZ问题解决没有?没有解决的话,透点气...
[解决办法]
工资到底要多少才满足?
http://www.jobhere.net/Article/ArticleShow.asp?ArticleID=6575
[解决办法]
主有有注意到大小?
SQL里不分大小
而C#里要分的
[解决办法]
if (this.comboBox1.Text == petnamee & this.textBox1.Text == petcodee)
if (this.comboBox1.Text == petnamee && this.textBox1.Text == petcodee)
少一个“&”你的if就不成立了