现在我正在向数据集添加数据,然后选择该数据的第一个表中的第一行和第一列。必须有一些更短的方法来做到这一点?
SqlCommand LoadCmd = new SqlCommand("Select start_tid from EKGDATA where ekgmaaleid = @ekgmaaleid", GlobalVariables.offCon);
LoadCmd.Parameters.AddWithValue("@ekgmaaleid", maaleid);
SqlDataAdapter da = new SqlDataAdapter(LoadCmd);
DataSet ds = new DataSet();
da.Fill(ds);
string dato = Convert.ToString(ds.Tables[0].Rows[0][0]);
最佳答案
Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
using(SqlCommand LoadCmd = new SqlCommand("Select start_tid from EKGDATA where ekgmaaleid = @ekgmaaleid", GlobalVariables.offCon))
{
LoadCmd.Parameters.AddWithValue("@ekgmaaleid", maaleid);
GlobalVariables.offCon.Open();
var result = LoadCmd.ExecuteScalar();
}
ExecuteScalar
返回object
类型的数据,您可以将其转换为您需要的类型。
(考虑将 using statement
与您的 SqlCommand
对象一起使用)
关于c# - 从 SQL Server 数据库中获取 1 个单元格的更简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20200143/