English 中文(简体)
选择一个项 sql
原标题:select one entry sql
  • 时间:2012-05-26 21:38:45
  •  标签:
  • c#
  • sql

我试图从数据库中只选择一个条目。 它正在返回一个 xml 文档对象, 我无法找到原因 。 最起码, 我的 Javascript 是这么告诉我的 。 我要它返回一个字符串, 即游戏名的下方 请求用户, 用户名= “ 这个用户 ” 。

          try {
            SqlConnection conn = new SqlConnection(@"Data asdfasdf;database=asdfsdfdf;User id=asdfasdfasdfPassword=asdfasdf;");

            SqlCommand getRequest = new SqlCommand("SELECT gameRequestUser FROM UserData Where userName= " + Session["userName"].ToString() + " ", conn);

            conn.Open();

            SqlDataReader reader = getRequest.ExecuteReader();

            while (reader.Read()) {
                user = reader.GetValue(0).ToString().Trim();
            }

            conn.Close();

            return user;
        } catch (Exception e) { return e.Message.ToString(); }
问题回答

您应该使用 ExecuteScalar 而不是 ExecuteReader :

user = (string)getRequest.ExecuteScalar();

甚至在您使用 SQL 服务器管理工作室检查查询结果之前, 还要在那里运行查询, 检查结果是否正确 。

总是使用参数, 避免太多问题( 字符串引用、 sql 注入等)

using(SqlConnection conn = new SqlConnection("yourconnectionstring"))
{
    SqlCommand getRequest = new SqlCommand("SELECT gameRequestUser FROM UserData Where " + 
                                           "userName=@user", conn); 
    conn.Open(); 
    getRequest.Parameters.AddWithValue("@user",Session["userName"].ToString()) 
    SqlDataReader reader = getRequest.ExecuteReader(); 
    while (reader.Read()) { 
        user = reader.GetValue(0).ToString().Trim(); 
    } 
}

您应该做的一件事是进入 SQL 服务器管理工作室, 尝试直接运行查询 :

SELECT gameRequestUser FROM UserData Where userName= this user 

说到这里,还有一件事需要记住, 你可以告诉SQL 最多一排回到你身边, 做一些事情,比如:

SELECT top 1 gameRequestUser FROM UserData Where userName= this user 

我希望这能帮上忙!

  1. 使用 安全 端口 1... 查询

    选择 TOP 1 游戏用户数据用户用户用户用户用户用户

  2. 使用 SqlCommand s deuteScalar () 方法, 而不是执行Reader (), 因为只需要返回一个字段值 。

    SqlCommand getRequest = new SqlCommand(....);
    ...
    string user = Convert.ToString(cmd.ExecuteScalar());





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签