简体   繁体   English

根据C#中的TextBox值从SQL Server检索数据

[英]Retrieve Data from SQL Server based on TextBox value in C#

I have a database with 4 columns HouseNo , Date , Time and IndoorTemp . 我有一个包含4列HouseNoDateTimeIndoorTemp In my C# Windows application I have 4 textboxes, one each for the above columns. 在我的C#Windows应用程序中,我有4个文本框,以上各列各一个。

The user enters the HouseNo, Date and Time values in the Text Box. 用户在文本框中输入房屋编号,日期和时间值。 The inputs here should be a part of my SQL Where Clause with the output as the Indoor Temperature. 此处的输入应该是我的SQL Where子句的一部分,输出应为室内温度。 This should then be displayed in the last text box. 然后,这应该显示在最后一个文本框中。

I have used this connection: 我已使用此连接:

    // Create a String to hold the database connection string.
    string sdwConnectionString = @"Data Source=IE1ADTBD5ZL1S\;Initial Catalog=ThermostatData2;Integrated Security=True";

    // Create a connection
    SqlConnection sdwDBConnection = new SqlConnection(sdwConnectionString);

I tried a code to do this but it failed. 我尝试执行此操作的代码,但失败了。 My code is, 我的代码是

cmd.IndoorTemp = "select * from ThermData where HouseNo = '" + HouseNovalue.Text + "' and Date = '" + Datevalue.Text + "' and Time = '" + Timevalue.Text + "' ";

How do i achieve the desired result. 我如何达到理想的结果。

I am new to C# and do not know much. 我是C#的新手,并不了解很多。 All the above code was also with the help of a few blogs. 上面的所有代码也是在一些博客的帮助下进行的。

Do please help me out on this one. 请帮我解决这个问题。 Also suggest a good book where i could learn to program C# with SQL Server. 还建议一本好书,让我可以学习如何使用SQL Server编程C#。 Thanks a lot in advance. 非常感谢。

Try this: 尝试这个:

using (SqlConnection c = new SqlConnection("your connection string"))
{
    c.Open();

    using (SqlCommand cmd = new SqlCommand("SELECT IndoorTemp FROM ThermData WHERE HouseNo = @HouseNo AND Date = @Date AND Time = @Time")
    {
        cmd.Parameters.AddWithValue("@HouseNo", HouseNovalue.Text);
        cmd.Parameters.AddWithValue("@Date", Datevalue.Text);
        cmd.Parameters.AddWithValue("@Time", Timevalue.Text);

        var indoorTemp = cmd.ExecuteScalar();
        Indoortemp.Text = indoorTemp;
    }
}

In this example you're building a new SqlConnection , but you're leveraging the using statement to ensure it's properly disposed. 在此示例中,您将构建一个新的SqlConnection ,但您将利用using语句来确保正确处理了它。 You are doing the same thing for the SqlCommand . 您正在为SqlCommand做同样的事情。 Then, you are leveraging parameterized statements to ensure you're safe from SQL Injection. 然后,您将利用参数化语句来确保您免受SQL注入的伤害。

And don't shy away from this approach even if it's just a personal project that nobody else uses because you practice how you play. 并且即使您只是一个个人项目,其他人也不会因为使用该方法练习游戏方式而回避这种方法。 If you don't use parameterized SQL when you practice you certainly won't when you play. 如果您在练习时不使用参数化SQL,则在玩游戏时肯定不会使用。

Finally, you're leveraging ExecuteScalar which returns the first column from the first row of the result set -and that's what you want. 最后,您利用ExecuteScalar从结果集的第一行返回第一列,这就是您想要的。

First of all use parametrized query: 首先使用参数化查询:

var cmd = new SqlCommand("select * from ThermData where HouseNo = @houseno and Date = @date and Time = @time", sdwDBConnection);
cmd.Parameters.AddWithValue("@houseno", HouseNovalue.Text);
cmd.Parameters.AddWithValue("@date", Datevalue.Text);
cmd.Parameters.AddWithValue("@time", Timevalue.Text);

to get results you can do: 要获得结果,您可以执行以下操作:

sdwDBConnection.Open();
var reader = cmd.ExecuteReader();

if (reader.Read()) //if there is at least one result
{
    //data from reader
    IndoorTemp.Text = reader["IndoorTemp"];  //access value returned for column IndoorTemp
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM