返回 int 而不是数据表

发布于 2024-12-18 19:42:49 字数 699 浏览 1 评论 0原文

我将如何更改下面的语法以返回 Int 而不是数据表。我不需要数据表,它只需在查询中返回一个值。整个数据访问的新事物。感谢您的帮助。

 public DataTable GetMemberID(string guid)
   {
       string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];

       //set up sql
       string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)";

       DataTable dt = new DataTable();
       using (SqlDataAdapter daObj = new SqlDataAdapter(StrSql, strConectionString))
       {
           daObj.SelectCommand.Parameters.Add("@GuidID", SqlDbType.Int);
           daObj.SelectCommand.Parameters["@GuidID"].Value = guid;
           //fill data table
           daObj.Fill(dt);
       }
       return dt;

   }

How would I change my syntax below to return an Int instead of a data table. I dont need a datatable its just one value that will be returned in the query. New to this whole data access thing. Thanks for your help.

 public DataTable GetMemberID(string guid)
   {
       string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];

       //set up sql
       string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)";

       DataTable dt = new DataTable();
       using (SqlDataAdapter daObj = new SqlDataAdapter(StrSql, strConectionString))
       {
           daObj.SelectCommand.Parameters.Add("@GuidID", SqlDbType.Int);
           daObj.SelectCommand.Parameters["@GuidID"].Value = guid;
           //fill data table
           daObj.Fill(dt);
       }
       return dt;

   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丢了幸福的猪 2024-12-25 19:42:49

您可以使用 SqlCommand 代替 SqlDataAdapter

int memberId = 0;
using (var connection = new SqlConnection(conectionString))
using (var command = new SqlCommand(StrSql, connection))
{
    command.Parameters.Add("@GuidID", SqlDbType.Int).Value = guid;
    memberId = (int) command.ExecuteScalar();
}

return memberId;

You can use SqlCommand instead of SqlDataAdapter:

int memberId = 0;
using (var connection = new SqlConnection(conectionString))
using (var command = new SqlCommand(StrSql, connection))
{
    command.Parameters.Add("@GuidID", SqlDbType.Int).Value = guid;
    memberId = (int) command.ExecuteScalar();
}

return memberId;
情深已缘浅 2024-12-25 19:42:49

使用 SqlCommandExecuteScalar 而不是填充 DataTable

string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)";
using(var cmd = new SqlCommand(sql, connection))
{
   cmd.Parameters.Add("@GuidID", SqlDbType.Int).Value = guid;
   return (int)cmd.ExecuteScalar();
}

Use SqlCommand and ExecuteScalar instead of filling a DataTable:

string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)";
using(var cmd = new SqlCommand(sql, connection))
{
   cmd.Parameters.Add("@GuidID", SqlDbType.Int).Value = guid;
   return (int)cmd.ExecuteScalar();
}
梦中的蝴蝶 2024-12-25 19:42:49
   public int GetMemberID(string guid) 
   { 
       string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"]; 

       //set up sql 
       string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)"; 

       DataTable dt = new DataTable(); 
       using (SqlDataAdapter daObj = new SqlDataAdapter(StrSql, strConectionString)) 
       { 
           daObj.SelectCommand.Parameters.Add("@GuidID", SqlDbType.Int); 
           daObj.SelectCommand.Parameters["@GuidID"].Value = guid; 
           //fill data table 
           daObj.Fill(dt); 
       } 
       return Convert.ToInt32(dt["MemberID"][0]); 

   } 
   public int GetMemberID(string guid) 
   { 
       string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"]; 

       //set up sql 
       string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)"; 

       DataTable dt = new DataTable(); 
       using (SqlDataAdapter daObj = new SqlDataAdapter(StrSql, strConectionString)) 
       { 
           daObj.SelectCommand.Parameters.Add("@GuidID", SqlDbType.Int); 
           daObj.SelectCommand.Parameters["@GuidID"].Value = guid; 
           //fill data table 
           daObj.Fill(dt); 
       } 
       return Convert.ToInt32(dt["MemberID"][0]); 

   } 
顾冷 2024-12-25 19:42:49

而不是:

 return dt;

使用这个:

if (dt.rows.count > 0)
   return (int)dt.rows[0][0];

声明还需要更改为:

 public int GetMemberID(string guid)

instead of:

 return dt;

use this:

if (dt.rows.count > 0)
   return (int)dt.rows[0][0];

The declaration also needs to be changed to:

 public int GetMemberID(string guid)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文