C#在if语句中使用查询结果 [英] C# use query result in a if statement
本文介绍了C#在if语句中使用查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个愚蠢且非常简单的问题,但是如何在if语句中使用查询结果。我正在查询表格,如果结果有一定值,我希望它执行一个函数。
代码到目前为止:
private void ButtonLogin_Click( object sender,RoutedEventArgs e)
{
string connectionSQL = @ server = xxxxxxxxxxx; user id = xxxxxxxxxxxx; password = xxxxxxxx; database = xxxxxxxx;
MySqlConnection cs = new MySqlConnection(connectionSQL);
cs.Open();
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter( 选择用户名,密码,来自Admin,cs);
MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);
da.Fill(ds);
}
我想在列类型的ds中对值进行if语句。
解决方案
< blockquote>现在你可以使用你的DataSet ds。
如果你使用表0和第0行的索引:
string user = ds.Tables [ 0 ]。行[ 0 跨度>];
string pass = ds.Tables [ 0 ]。行[ 1 ];
或者您可以使用表格和行的名称:
string user = ds.Tables [ MyTableName]。行[ 用户名];
string pass = ds.Tables [ MyTableName]。行[ 密码];
祝你好运,
Edo
*请注意你错过了关闭连接,[ cs.Close(); ]
This is probably a silly and very easy question but how do you use a result from a query in a if statement. I am querying a table and if the result has a certain value I want it to carry out a function.
Code so far:
private void ButtonLogin_Click(object sender, RoutedEventArgs e)
{
string connectionSQL = @"server=xxxxxxxxxxx;user id=xxxxxxxxxxxx;password=xxxxxxxx;database=xxxxxxxx";
MySqlConnection cs = new MySqlConnection(connectionSQL);
cs.Open();
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter("Select username, password, type from Admin", cs);
MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);
da.Fill(ds);
}
I want to do a if statement on values in ds in column type.
解决方案
Now you can use your DataSet ds.
if you use indexes like table 0 and row 0:
string user = ds.Tables[0].Rows[0]; string pass = ds.Tables[0].Rows[1];
or you can use names of the tables and rows:
string user = ds.Tables["MyTableName"].Rows["Username"]; string pass = ds.Tables["MyTableName"].Rows["Password"];
Good luck,
Edo
* Note that you missed closing the connection, [cs.Close();]
这篇关于C#在if语句中使用查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文