各位大虾,我是个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"; // 创建DataSet命令对象
// 和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语句还是不执行.各位大虾麻烦该看看吧.帮帮我吧
我用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"; // 创建DataSet命令对象
// 和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语句还是不执行.各位大虾麻烦该看看吧.帮帮我吧
解决方案 »
- 在DataGridViewComboBoxColumn 如何移除当前值
- c# mapx添加动态图层的错误
- C#有谁做过超市里用的进销存系统?
- vs2005能否在编译后配置客户端的web services的url
- 多态 重载 重写 覆盖??
- DXperience因为有的控件想增加功能(比如增加一个按钮),有了源码能修改重新编译吗?
- 如何将ListView控件选中的项向上移动或向下移动
- 关于WebConfigurationManager读取配置文件问题
- Global.asax里的Application_Error里面能不能定义对于HTTP404错误的处理代码???
- 要做项目经理了,请问项目经理的职责(需要做什么工作),需要懂那些知识?希望有经验的项目经理回答!
- C# 窗体 相吸 想kmp那样或者 千千静听那样的~~~不知道怎么实现~~
- 如何把XML文件内容绑定到DataSet里面去
说是从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)
petnamee = datatable.Rows[i]["petname"].ToString();
petcodee = datatable.Rows[i]["petcode"].ToString();
这2个变量能取到值不?
petnamee = datatable.Rows[i]["petname"].ToString();
petcodee = datatable.Rows[i]["petcode"].ToString();
这2个变量能取到值不?
能取到值,还能把值赋给别的东西,例如label的text
//注意这里:应该是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();
// ...
}
--->
if (this.comboBox1.Text == petnamee && this.textBox1.Text == petcodee)
改为:
if ((this.comboBox1.Text == petnamee) && (this.textBox1.Text == petcodee))
若还是不行,估计是你的combobox1与textbox1取出来的值有问题了,
=========================================
改为if(this.comboBox1.Text.ToString().equals(petnamee) & this.textBox1.Text.ToString().equals(petcodee))=========================================
还有一个问题就是LZ的代码有问题,那个DataSet 实例的值为空。因为你的代码都没有去执行,所以,无论你怎么去取值,怎么去比较,都是没有用的...
http://www.jobhere.net/Article/ArticleShow.asp?ArticleID=6575
SQL里不分大小寫
而C#里要分的
少一个“&”你的if就不成立了
try 去掉空格if (this.comboBox1.Text.Trim() == petnamee.Trim() && this.textBox1.Text.Trim() == petcodee.Trim())感谢大家的支持!
其实昨天sqllong(阿秋)已经提醒我了,都怪我没注意.谢谢啊