简单涩;
public string getNameByID(int id)
{
//连接语句
string cons="";连接语句(连接你的数据库的语句);
//sql语句
string sql=" select username from [userinfo] where userid=@id"
//创建连接
sqlconnection con=new sqlconnection(cons);
//创建适配器
sqlcommand cmd=new sqlcommand();
//配置参数
cmd.parmeters.add("@id",sqldbtype.int).value=id;
//打开连接
con.open();
//接受执行操作后返回的数据
string name=cmd.executeNoQuery().tostring();
//返回结果
return name;
}
在外面调用方法;
string Name=getNameByID(id--想要查找名字的id);
你的意思是想获得username的值:
你可以这样:
使用datatable:
string sqltext=" select username form userinfo where userid = "xx""
private SqlConnection conn = new SqlConnection("连接字符串");
DataTable dt = null;
SqlCommand com = null;
SqlDataAdapter sda = null;
public DataTable ExcuteSqlQueryText(string sqltext)
{
dt = new DataTable();
if (conn.State != ConnectionState.Open)
conn.Open();
com = new SqlCommand(sqltext, conn);
sda = new SqlDataAdapter(com);
sda.Fill(dt);
conn.Close();
return dt;
}
这个方法返回的是一个datatable 并且在datatable中的第一行就会是你想要的数据
string username=dt.rows[0]["username"];
这个username就会是你想要的数据了
你的意思是想获得username的值:
你可以这样:
使用datatable:
string sqltext=" select username form userinfo where userid = "xx""
private SqlConnection conn = new SqlConnection("连接字符串");
DataTable dt = null;
SqlCommand com = null;
SqlDataAdapter sda = null;
public DataTable ExcuteSqlQueryText(string sqltext)
{
dt = new DataTable();
if (conn.State != ConnectionState.Open)
conn.Open();
com = new SqlCommand(sqltext, conn);
sda = new SqlDataAdapter(com);
sda.Fill(dt);
conn.Close();
return dt;
}
这个方法返回的是一个datatable 并且在datatable中的第一行就会是你想要的数据
string username=dt.rows[0]["username"];
这个username就会是你想要的数据了
当button1点击的时候,先清理掉dataSet11中的所有表,然后重新填充数据源dataSet11的表student,绑定 datagridview,
当button2点击的时候,你只是定义了一个查询字符串,数据源dataSet11的数据表student内容并没有发生改变,你再次绑定的 datagridview话,没有任何变化的。建议你在button2点击后,根据新的sql,重新填充数据源 并重新 绑定到datagridview即可。
select name from where id="";
textbox2.text=bll.ExecuteScalar(sql);