使用datareader.C获取Sql查询结果# [英] Get Sql query result with datareader.C#
问题描述
大家好,
我试图用数据库表中的值填充一些文本框。当我在文本框中写一个名字时,我用它作为参数对于查询,查询结果应该显示不同文本框中的值,例如一个文本框中的姓氏,另一个文本框中的id等。有没有办法做到这一点?
我不是英文,所以我希望你理解我。
这里是我的代码的一部分,我检查输入是否被按下的部分,连接到db,下面是3种方式的datareader,但没有一个显示任何东西:
private void CheckKeys( object sender,System.Windows .Forms.KeyPressEventArgs e)
{
string _connString = String .Format( System.Globalization.CultureInfo.InvariantCulture,
@ 数据源= {0} \SQL_CE.sdf,Path.GetDirectoryName(System.Reflection) 。.Assembly.GetExecutingAssembly()的GetName()基本代码));
SqlCeConnection _connection = new SqlCeConnection(_connString);
if (e.KeyChar ==( char ) 13 )
{
SqlCeCommand command = new SqlCeCommand( SELECT surname,ID FROM Data其中name = @ name;,_ connect);
command.Parameters.Add( @ name,txt_name.Text );
_connection.Open();
SqlCeDataReader rdr = command.ExecuteReader();
if (rdr.Read())
{
txt_surname.Text = Convert.IsDBNull (rdr [ 姓])? :Convert.ToString(rdr [ 姓]);
txt_ID.Text = rdr [ ID]。ToString();
}
rdr.Close();
}
这不是确切的代码,但它是一个起点。你需要的所有位都在那里。
使用(SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
使用(SqlCommand cmd = new SqlCommand( SELECT描述FROM myTable WHERE ID = @ ID,con))
{
cmd.Parameters.AddWithValue( @ ID,myId);
使用(SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string desc =( string )reader [ description];
Console.WriteLine( ID:{0} \ n {1}, myId,desc);
}
}
}
}
以下是示例代码:
此 .textBoxName.KeyPress + = new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
private void CheckKeys( object sender,System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == ( char ) 13 )
{
// 然后按下Enter键
string ConnectionString = server = xxx; userid = aaa; + pwd = bbb; database = northwind;
con = new SqlConnection(ConnectionString);
con.Open();
string CommandText = SELECT SurName +
FROM Employees +
WHERE(Name LIKE @) + textBoxName.Text;
cmd = new SqlCommand(CommandText);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
textSurName = rdr [ SurName]。ToString()
}
}
}
Hello everyone,
Im trying to fill some textboxes with values from a database table.when i write a name in a textbox,i use it as a parameter for the query,the query result should show the values in different textboxes,for example the surname in one textbox,the id in another etc.Is there any way to do this??
im not english,so i hope u understand me.
here is part of my code,the part where i check if enter is pressed,connect to the db,below are 3 ways for datareader but none of them shows anything:
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
string _connString = String.Format(System.Globalization.CultureInfo.InvariantCulture,
@"Data Source = {0}\SQL_CE.sdf", Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase));
SqlCeConnection _connection = new SqlCeConnection(_connString);
if (e.KeyChar == (char)13)
{
SqlCeCommand command = new SqlCeCommand("SELECT surname, ID FROM Data where name=@name;", _connection);
command.Parameters.Add("@name", txt_name.Text);
_connection.Open();
SqlCeDataReader rdr = command.ExecuteReader();
if(rdr.Read())
{
txt_surname.Text=Convert.IsDBNull(rdr["Surname"]) ? "" : Convert.ToString(rdr["Surname "]);
txt_ID.Text = rdr["ID"].ToString();
}
rdr.Close();
}
This isn't the exact code, but it is a starting point. All the bits you need are there.
using (SqlConnection con = new SqlConnection(strConnect)) { con.Open(); using (SqlCommand cmd = new SqlCommand("SELECT description FROM myTable WHERE ID=@ID", con)) { cmd.Parameters.AddWithValue("@ID", myId); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { string desc = (string) reader["description"]; Console.WriteLine("ID: {0}\n {1}", myId, desc); } } } }
Here is the sample code:
this.textBoxName.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys); private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyChar == (char)13) { // Then Enter key was pressed string ConnectionString = "server=xxx;userid=aaa;"+ "pwd=bbb;database=northwind"; con = new SqlConnection(ConnectionString); con.Open(); string CommandText = "SELECT SurName" + " FROM Employees" + " WHERE (Name LIKE @)" + textBoxName.Text; cmd = new SqlCommand(CommandText); rdr = cmd.ExecuteReader(); while(rdr.Read()) { textSurName = rdr["SurName"].ToString() } } }
这篇关于使用datareader.C获取Sql查询结果#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!