【问题标题】:Trying to access a SQL Server database to display a single row in c#尝试访问 SQL Server 数据库以在 C# 中显示单行
【发布时间】:2015-05-14 04:12:44
【问题描述】:

我正在尝试访问 SQL Server 数据库并使用 C# 显示单行。我很难弄清楚如何正确地做到这一点。请帮忙。谢谢你。

public Form1()
{
    InitializeComponent();

    string connectionString = "Data Source=localhost \\SqlExpress;Initial Catalog=MMABooks;" + "Integrated Security=True";
    SqlConnection connection = new SqlConnection(connectionString);

    string selectStatement = "SELECT ProductCode " + "FROM Products " + "WHERE ProductCode = @ProductCode";
    SqlCommand selectCommand = new SqlCommand(selectStatement, connection);

    selectCommand.Parameters.Add("@ProductCode");

    connection.Open();

    SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.SingleRow);

    reader.Read();

    txtDisplay.Text = reader["ProductCode"].ToString();
}

【问题讨论】:

  • 那么问题是什么?

标签: c# sql sql-server database ado.net


【解决方案1】:

我试图纠正您代码中的一些问题:

连接字符串中有一个奇怪的空格。

不需要连接两个字符串,写一个字符串就可以了。

您没有设置参数的值。我使用了 AddWithValue 方法。

你没有关闭阅读器和阅读后的连接。

 string connectionString = "Data Source=localhost\\SqlExpress;Initial Catalog=MMABooks;Integrated Security=True";
 SqlConnection connection = new SqlConnection(connectionString);

 string selectStatement = "SELECT ProductCode FROM Products WHERE ProductCode = @ProductCode";
 SqlCommand selectCommand = new SqlCommand(selectStatement, connection);

 selectCommand.Parameters.AddWithValue("@ProductCode", "XYZ");//Or whatever your parameter value is
 connection.Open();

 SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.SingleRow);

 reader.Read();

 txtDisplay.Text = reader["ProductCode"].ToString();

 reader.Close();

 connection.Close();

无论如何,代码中的逻辑根本没有意义。您使用参数作为键进行查询,然后显示参数值。这使得查询完全没用!

【讨论】:

  • 我对如何正确使用 AddWithvalue 方法感到困惑,我知道@productCode 指向我要访问的行,但我想把什么作为参数值。我基本上只是希望产品代码显示在 txtDisplay.Text 中。
  • 您在执行查询之前是否获得了产品代码值?或者您只想显示第一行?在查询中使用参数然后将其显示为唯一的输出是没有意义的
  • 假设其中一个产品代码值读取 A3CS,这就是我希望它显示的行。我可以为 A3CS 创建一个字符串并将其作为参数值放置,这样它就知道我要的是哪一个?
  • 是的,但也许您想在 texbox 中显示一些其他信息。相信我,进行查询然后只显示参数真的没有多大意义。
  • 好的。让我试试。谢谢。但是 SQL 数据库会知道我正在尝试使用字符串来描述我也尝试指向的行吗?
【解决方案2】:

看起来您正在将参数 productcode 传递给查询并返回与结果相同的结果。请把情节讲清楚。如果您需要传递产品代码并从表中获取相同的代码,您可以做的是:

 public Form1()
{
    InitializeComponent();

    string connectionString = "Data Source=localhost \\SqlExpress;Initial Catalog=MMABooks;" + "Integrated Security=True";
    Object returnValue;
    SqlConnection connection = new SqlConnection(connectionString);

    string selectStatement = "SELECT Top 1 ProductCode " + "FROM Products " + "WHERE ProductCode = @ProductCode";
    SqlCommand selectCommand = new SqlCommand(selectStatement, connection);

    selectCommand.Commandtype = CommandType.Text;
    connection.Open();

    returnValue = selectCommand.ExecuteScalar();
    connection.Close();
    txtDisplay.Text = returnValue.ToString();

}

【讨论】:

  • 我试过你的一段代码,它说找不到Commandtype,我错过了一个使用语句来访问它吗?我正在使用 sql.Data.SqlClient.
  • 我发现这只是命令类型的拼写错误,但现在我收到此错误必须声明标量变量“@ProductCode”。当我尝试调试它时。
  • 也尝试使用这个:使用 System.Data;使用 System.Data.SqlClient;您需要将产品变量传递给函数并在内联选择查询中使用变量名称,或者只需为@product 代码赋值
  • 它起作用了,当我为 selectCommand.Parameter,AddWithValue () 分配一个字符串时,它告诉它我想要的行。谢谢你们。
  • 只要验证所有人给出的答案,如果它对你有用。它可能会帮助其他尝试过这个的人。谢谢
【解决方案3】:

要分配变量,您可以使用此代码:请确保数据库中 productcode 的值。在这里,我将数据类型指定为 varchar ,如果它不只是用 varchar 替换数据类型。

public Form1(string productCodevalue){
InitializeComponent();

string connectionString = "Data Source=localhost \\SqlExpress;Initial Catalog=MMABooks;" + "Integrated Security=True";
Object returnValue;
SqlConnection connection = new SqlConnection(connectionString);

string selectStatement = "SELECT Top 1 ProductCode " + "FROM Products " + "WHERE ProductCode = @ProductCode";
SqlCommand selectCommand = new SqlCommand(selectStatement, connection);

selectCommand.Commandtype = CommandType.Text;
selectCommand.Parameters.Add("ProductCode", SqlType.VarChar).Value = productCodevalue;
connection.Open();

returnValue = selectCommand.ExecuteScalar();
connection.Close();
txtDisplay.Text = returnValue.ToString();

}`

【讨论】:

    【解决方案4】:
    public Form1(string productCodevalue){
    InitializeComponent();
    
    string connectionString = "Data Source=localhost \\SqlExpress;Initial Catalog=MMABooks;" + "Integrated Security=True";
    Object returnValue;
    SqlConnection connection = new SqlConnection(connectionString);
    
    string selectStatement = "SELECT Top 1 ProductCode " + "FROM Products " + "WHERE ProductCode = @ProductCode";
    SqlCommand selectCommand = new SqlCommand(selectStatement, connection);
    
    selectCommand.Commandtype = CommandType.Text;
    selectCommand.Parameters.Add("ProductCode", SqlType.VarChar).Value = productCodevalue;
    connection.Open();
    
    returnValue = selectCommand.ExecuteScalar();
    connection.Close();
    txtDisplay.Text = returnValue.ToString();}` 
    

    【讨论】:

    • 确保数据库中产品代码的数据类型为varchar。在代码中,如果不只是用 varchar 替换数据类型,我将其作为 varchar 并尝试此
    【解决方案5】:
     string connectionString = "Data Source=localhost\\SqlExpress;Initial Catalog=MMABooks;" + "Integrated Security=True";
            SqlConnection connection = new SqlConnection(connectionString);
    
    
            string selectStatement = "SELECT ProductCode " + "FROM Products " + "WHERE ProductCode = @ProductCode";
            SqlCommand selectCommand = new SqlCommand(selectStatement, connection);
    
            string code = "DB1R";
    
            selectCommand.Parameters.AddWithValue("@ProductCode", code);  //productcode points to the column, and code points the row
            connection.Open();
    
            SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.SingleRow);
    
            reader.Read();
    
            txtDisplay.Text = reader["ProductCode"].ToString();
    
            connection.Close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-23
      • 2012-06-21
      • 1970-01-01
      • 2021-09-22
      • 2014-01-18
      • 2016-10-18
      • 1970-01-01
      相关资源
      最近更新 更多