尝试访问 SQL 服务器数据库以在 c# 中显示单行

Trying to access a SQL Server database to display a single row in c#

我正在尝试访问 SQL 服务器数据库并使用 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();
}

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

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

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

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

您没有在阅读后关闭 reader 和连接。

 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();

反正代码里的逻辑一点意义都没有。您使用参数作为键进行查询,然后显示参数值。这使得查询完全无用!

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

 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();

}

要分配变量,您可以使用此代码:请确保数据库中产品代码的值。在这里,我将数据类型指定为 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();

}`

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();}` 
 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();