获取sqlcommand的值 [英] get value of sqlcommand
问题描述
亲爱的,
这是我的代码:
SqlDataReader dReader;
SqlConnection conn = new SqlConnection( @ 数据源= .\sql2008;初始目录= jelvehsadb;集成安全性=真;);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = @ 从work_with_us中选择响应,其中national_code = @ CustomerID跨度>;
comm.Parameters.AddWithValue( CustomerID, 00488 跨度>);
comm.Connection.Open();
dReader = comm.ExecuteReader();
if (dReader.HasRows == true )
{
TxtShow.Text = ???;
}
}
我的问题是如何从dreader获得我的回复?
结果必须在txtshow上显示。
谢谢
尝试:
if (dReader.Read())
{
TxtShow.Text =( string )reader [ response];
}
但在步骤2中我的sqlcommand返回一个值,它与响应列无关,
所以我想在datareader上准备好访问该值,你可以帮我看看如何访问这些数据,
谢谢dera OriginalGriff
是的,它是!
当你的问题发生时SELECT,SQL返回与WHILE语句匹配的所有行,但仅返回您指定的列。所以如果你有一个包含三列的表:
Id UserName UserAddress
7 Fred Home
8 Joe Away
9 Bert Holiday小屋你发出一个SELECT:
SELECT UserName,UserAddress FROM MyTable WHERE Id = 8
然后,您需要对DataReader进行两次访问,以读取您要求的行的两列中的每一列:
string userName =( string )reader [ 用户名跨度>];
string userAddr =( string )reader [ UserAddress];
在你的情况下,你要求(我假设)一行(national_code为488)和一栏:回复。
这有意义吗?或者我们在这里交叉目的?
你好会员,
你可以修改你的代码,如下所示。 />
SqlDataReader dReader;
SqlConnection conn = new SqlConnection( @ Data Source = .\sql2008; Initial Catalog = jelvehsadb; Integrated Security = True;);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = @ 从work_with_us中选择响应,其中national_code = @ CustomerID跨度>;
comm.Parameters.AddWithValue( CustomerID, 00488 跨度>);
comm.Connection.Open();
dReader = comm.ExecuteReader();
// SqlDataReader的默认位置在第一条记录之前。
// 因此,您必须调用Read才能开始访问任何数据。
if (dReader.Read()){
TxtShow.Text = dReader.GetString( 0 跨度>);
}
问候,
你可以这样做:
SqlDataReader drmax1;
drmax = comm.ExecuteReader();
drmax.Read();
TxtShow.Text = System.Convert.ToString((System.Convert.ToInt32(drmax [ response 跨度>]的ToString()));
hi dear's,
this is my code:
SqlDataReader dReader;
SqlConnection conn = new SqlConnection(@"Data Source=.\sql2008;Initial Catalog=jelvehsadb;Integrated Security=True;");
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = @"Select response from work_with_us where national_code =@CustomerID";
comm.Parameters.AddWithValue("CustomerID", 00488);
comm.Connection.Open();
dReader = comm.ExecuteReader();
if (dReader.HasRows == true)
{
TxtShow.Text = ???;
}
}
my problem is how i can get my response vlaue from dreader??
the result must show on txtshow.
thanks
Try:
if (dReader.Read()) { TxtShow.Text = (string) reader["response"]; }
"but in step 2 when my sqlcommand return a value,it's not related to the response column,
so i want access to that value is now ready on the datareader,may you help me how i can access to this data,
thanks dera OriginalGriff"
Yes, it is!
When your issue a SELECT, SQL returns all the rows that match the WHILE statement, but only the columns you specify. So if you have a table with three columns:
Id UserName UserAddress 7 Fred Home 8 Joe Away 9 Bert Holiday cottageAnd you issue a SELECT:
SELECT UserName, UserAddress FROM MyTable WHERE Id=8
Then you need to do two accesses to your DataReader to read each of the two columns of the row that you have asked for:
string userName = (string) reader["UserName"]; string userAddr = (string) reader["UserAddress "];
In your case, you are asking for (I assume) a single row (where the national_code is 488) and a single column: response.
Does that make sense? Or are we talking at cross purposes here?
Hello Member,
You can modify your code as shown below.
SqlDataReader dReader; SqlConnection conn = new SqlConnection(@"Data Source=.\sql2008;Initial Catalog=jelvehsadb;Integrated Security=True;"); SqlCommand comm = new SqlCommand(); comm.Connection = conn; comm.CommandText = @"Select response from work_with_us where national_code =@CustomerID"; comm.Parameters.AddWithValue("CustomerID", 00488); comm.Connection.Open(); dReader = comm.ExecuteReader(); // The default position of the SqlDataReader is before the first record. // Therefore, you must call Read to begin accessing any data. if (dReader.Read()) { TxtShow.Text = dReader.GetString(0); }
Regards,
you can do something like this :
SqlDataReader drmax1; drmax = comm.ExecuteReader(); drmax.Read(); TxtShow.Text = System.Convert.ToString((System.Convert.ToInt32(drmax["response"].ToString()));
这篇关于获取sqlcommand的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!