SqlConnection conn = new SqlConnection("Server=(local);DataBase=test;Uid=sa;Pwd=123");
SqlDataReader r ;
SqlCommand cmd = new SqlCommand("SELECT adjustdate FROM plupos AS WHERE barcode = xxx ",conn); ///你的SQL语句
conn.Open();
r = cmd.ExecuteReader(); //执行SQL语句
if (r.Read()) //如果sql查询到了数据
string ss = (r.GetInt32(0).ToString()); // 用一个变量存储
conn.Close();
这个是只返回一个变量的实例。
如果你的条件查询出多条数据的话,这个就不对了,用新的方法:数据集存放: ds.table[表名].rows[第几行][第几列]
(例如:int ii = Convert.ToInt32(ds.table[0].rows[0][1])
如果是多行结果也可以使用DataReader
DataReader reader = sqlcmd.ExecuteReader();
while ( reader.Read() )
{
// 每行的数据处理代码...
// 如要取字段一的值
object obj = reader[0];
// 如果知道字段名,如"name"
string name = (string)reader["name"];
//...
}
reader.Close();
2楼写的没错
但用ExecuteScalar就够了,而且效率更高
Dim myDate As string
Dim conn As SqlConnection = New SqlConnection("Server=(local);DataBase=test;Uid=sa;Pwd=123")
Dim sql As String = "SELECT adjustdate FROM plupos AS WHERE barcode = xxx "
Dim cmd As New SqlCommand(sql, conn)
conn.open
myDate=cmd.ExecuteScalar().tostring
conn.Close()
恩。。前台传啊。