我有一个SQL Server数据库文件MsUser.mdf和一个表MsAccount有5列:

userID   accountID   accountName   accountBalance   imageLocation

我需要找到所选的accountBalance位置,并在accountID = combobox中显示它。labelBalance._textAccountBalancedecimalaccountID
我在combobox事件选择索引处编写了代码。
谢谢你的帮助。
      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
          string selected = comboBox2.SelectedItem.ToString();//typ0000001-cake
          int position = selected.IndexOf("-");
          string accountID = selected.Substring(0,position);//typ0000001
          SqlDataAdapter sdaUserID = new SqlDataAdapter("Select Count(accountBalance),accountBalance From MsAccount where accountID='" +accountID+"'", cn);
          DataTable dt1 = new DataTable();
          sdaUserID.Fill(dt1);
          lblBalance.text = dt1.Rows[0][1].ToString();

      }

最佳答案

我很高兴你的代码工作了。一般来说,最好创建一个参数化查询,但如果安全性不是主要问题,那么只需一个简单的select sql字符串就可以完成这项工作(与您的情况一样)。
关于一些性能优化,我建议使用String.Concat(string1, string2)而不是string1+string2方法,因此最好修改代码中的行,如下所示:

SqlDataAdapter sdaUserID = new SqlDataAdapter(String.Concat ("Select Count(accountBalance),accountBalance From MsAccount where accountID='",accountID, "'"), cn);

谨致问候,

关于c# - 如何在.mdf数据库上执行参数化的选择查询并显示列值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27957158/

10-11 01:34