序号 ID name age
010 1234 tom 45
011 1236 ken 23
012 1269 bom 60
当输入010点击按钮时,文本框显示数据为: 010 1234 tom 45
若输入011点击按钮时,文本框显示数据为:011 1236 ken 23
数据库是SQL Server 2005 ,开发平台是Visual Studio 2010.
8 个解决方案
#1
自己学的不好,麻烦大家了
#2
Just Like This
string conn_str = @"Data Source=(local);Initial Catalog=数据库名;UserID=用户名;Password=密码";
string show_str = string.Empty;
using (SqlConnection conn = new SqlConnection(conn_str))
{
conn.Open();
string sql = string.Format("select * from Table where 序号='{0}'",this.textBox1.Text.Trim());
using (SqlCommand com = new SqlCommand(sql,conn))
{
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
show_str = string.Format("{0} {1} {2} {3}",dr[0].ToString(),dr[1].ToString(),dr[2].ToString(),dr[3].ToString());
}
dr.Close();
dr.Dispose();
}
}
this.textBox2.Text = show_str;//显示
#3
序号是主键,直接将文本框的值绑定根据主键查询出来在datagridview显示的各个单元格值。
这样应该可以吧
这样应该可以吧
#5
string conn_str = @"Data Source=(local);Initial Catalog=数据库名;UserID=用户名;Password=密码";
string show_str = string.Empty;
using (SqlConnection conn = new SqlConnection(conn_str))
{
conn.Open();
string sql = string.Format("select * from Table where 序号='{0}'",this.textBox1.Text.Trim());
using (SqlCommand com = new SqlCommand(sql,conn))
{
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
show_str = string.Format("{0} {1} {2} {3}",dr[0].ToString(),dr[1].ToString(),dr[2].ToString(),dr[3].ToString());
}
dr.Close();
dr.Dispose();
}
}
this.textBox2.Text = show_str;//显示
我运行了这段代码,vs2010没有报错,当我输入010查询表格数据时文本框内什么数据也没有
string show_str = string.Empty;
using (SqlConnection conn = new SqlConnection(conn_str))
{
conn.Open();
string sql = string.Format("select * from Table where 序号='{0}'",this.textBox1.Text.Trim());
using (SqlCommand com = new SqlCommand(sql,conn))
{
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
show_str = string.Format("{0} {1} {2} {3}",dr[0].ToString(),dr[1].ToString(),dr[2].ToString(),dr[3].ToString());
}
dr.Close();
dr.Dispose();
}
}
this.textBox2.Text = show_str;//显示
我运行了这段代码,vs2010没有报错,当我输入010查询表格数据时文本框内什么数据也没有
#6
你的连接数据库的字符串是否正确,你的查询语句Table 是否对应,都是基本操作
#7
String connectionString = @"server=localhost; database=master;uid=sa;pwd=123456; ";
string show_str = string.Empty;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = string.Format("select userID,userName from tUSER where 4='{0}'", this.textBox_output.Text.Trim());
using (SqlCommand com = new SqlCommand(sql, connection))
{
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
show_str = string.Format("{0} {1} {2} {3}", dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString());
}
dr.Close();
dr.Dispose();
}
}
this.textBox_output.Text = show_str;//显示
这是修改以后代码
string show_str = string.Empty;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = string.Format("select userID,userName from tUSER where 4='{0}'", this.textBox_output.Text.Trim());
using (SqlCommand com = new SqlCommand(sql, connection))
{
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
show_str = string.Format("{0} {1} {2} {3}", dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString());
}
dr.Close();
dr.Dispose();
}
}
this.textBox_output.Text = show_str;//显示
这是修改以后代码
#8
textBox_output是显示查询数据的文本框
#1
自己学的不好,麻烦大家了
#2
Just Like This
string conn_str = @"Data Source=(local);Initial Catalog=数据库名;UserID=用户名;Password=密码";
string show_str = string.Empty;
using (SqlConnection conn = new SqlConnection(conn_str))
{
conn.Open();
string sql = string.Format("select * from Table where 序号='{0}'",this.textBox1.Text.Trim());
using (SqlCommand com = new SqlCommand(sql,conn))
{
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
show_str = string.Format("{0} {1} {2} {3}",dr[0].ToString(),dr[1].ToString(),dr[2].ToString(),dr[3].ToString());
}
dr.Close();
dr.Dispose();
}
}
this.textBox2.Text = show_str;//显示
#3
序号是主键,直接将文本框的值绑定根据主键查询出来在datagridview显示的各个单元格值。
这样应该可以吧
这样应该可以吧
#4
你这不就是个基本的查询吗
http://www.cnblogs.com/dongdonghuihui/archive/2009/08/13/1545446.html
你连百度的能力都没有吗
你连百度的能力都没有吗
#5
string conn_str = @"Data Source=(local);Initial Catalog=数据库名;UserID=用户名;Password=密码";
string show_str = string.Empty;
using (SqlConnection conn = new SqlConnection(conn_str))
{
conn.Open();
string sql = string.Format("select * from Table where 序号='{0}'",this.textBox1.Text.Trim());
using (SqlCommand com = new SqlCommand(sql,conn))
{
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
show_str = string.Format("{0} {1} {2} {3}",dr[0].ToString(),dr[1].ToString(),dr[2].ToString(),dr[3].ToString());
}
dr.Close();
dr.Dispose();
}
}
this.textBox2.Text = show_str;//显示
我运行了这段代码,vs2010没有报错,当我输入010查询表格数据时文本框内什么数据也没有
string show_str = string.Empty;
using (SqlConnection conn = new SqlConnection(conn_str))
{
conn.Open();
string sql = string.Format("select * from Table where 序号='{0}'",this.textBox1.Text.Trim());
using (SqlCommand com = new SqlCommand(sql,conn))
{
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
show_str = string.Format("{0} {1} {2} {3}",dr[0].ToString(),dr[1].ToString(),dr[2].ToString(),dr[3].ToString());
}
dr.Close();
dr.Dispose();
}
}
this.textBox2.Text = show_str;//显示
我运行了这段代码,vs2010没有报错,当我输入010查询表格数据时文本框内什么数据也没有
#6
你的连接数据库的字符串是否正确,你的查询语句Table 是否对应,都是基本操作
#7
String connectionString = @"server=localhost; database=master;uid=sa;pwd=123456; ";
string show_str = string.Empty;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = string.Format("select userID,userName from tUSER where 4='{0}'", this.textBox_output.Text.Trim());
using (SqlCommand com = new SqlCommand(sql, connection))
{
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
show_str = string.Format("{0} {1} {2} {3}", dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString());
}
dr.Close();
dr.Dispose();
}
}
this.textBox_output.Text = show_str;//显示
这是修改以后代码
string show_str = string.Empty;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = string.Format("select userID,userName from tUSER where 4='{0}'", this.textBox_output.Text.Trim());
using (SqlCommand com = new SqlCommand(sql, connection))
{
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
show_str = string.Format("{0} {1} {2} {3}", dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString());
}
dr.Close();
dr.Dispose();
}
}
this.textBox_output.Text = show_str;//显示
这是修改以后代码
#8
textBox_output是显示查询数据的文本框