在C#中处理SQL数据

mor*_*red 1 c# sql visual-studio

namespace iss_farmacie_spitali
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlDataReader reader = null;
            DataTable table = null;

            try
            {
                sqlConnection1.Open();
            }

            catch(Exception f)
            {
                Console.WriteLine(e.ToString());
            }
            sqlCommand1.Parameters.AddWithValue("@user",textBox1.Text);
            sqlCommand1.Parameters.AddWithValue("@pass",textBox2.Text);
            sqlCommand1.CommandText = "SELECT id,parola FROM ANGAJAT WHERE id='@user' AND parola='@pass'";

            reader = sqlCommand1.ExecuteReader();
            table.Load(reader);  


            sqlConnection1.Close();
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

所以我想在这里做忠实的登录,但我一直得到空引用错误.我需要帮助的是了解如何处理从sql获得的数据,以及如何使它可用作字符串和诸如此类的东西.

Jon*_*eet 5

这里有很多问题.

一个是你没有显示在哪里sqlCommand1或被sqlConnection1宣布或初始化.我的猜测是它们都是null,这就是你得到异常的原因.他们应该被宣布和方法中初始化,使用using的语句,使他们得到适当的配置:

using (var connection = new SqlConnection(...))
{
    connection.Open();
    using (var command = new SqlComand(...))
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

然后就是这个问题.

SELECT id,parola FROM ANGAJAT WHERE id='@user' AND parola='@pass'
Run Code Online (Sandbox Code Playgroud)

你有引号的事实意味着你正在寻找一个确切的ID @user- 而不是@user参数的值.你要:

SELECT id,parola FROM ANGAJAT WHERE id=@user AND parola=@pass
Run Code Online (Sandbox Code Playgroud)

编辑:如上所述,初始化你的DataTable:)


Par*_*ram 5

在你的代码table变量已被初始化,null所以它将抛出NullReferenceException.

在您的代码中进行以下更正 -

  1. 初始化table变量
  2. 初始化SqlConnection并在使用using后使用对象清理
  3. 初始化SqlCommand并在使用using后使用对象清理
  4. connection string
  5. 使用using后使用对象清理reader

AS -

private void button1_Click(object sender, EventArgs e) 
{
    SqlDataReader reader = null;

    //Corrections
    DataTable table = new DataTable(); //#1 : Initialize Table

    using(SqlConnection sqlConnection1 = new SqlConnection()) //#2: Initialize SqlConnection & make use of `using` for object cleanup after use
    using(SqlCommand sqlCommand1 = new SqlCommand()) //#3: Initialize SqlCommand & make use of `using` for object cleanup after use
    {

        sqlConnection1.ConnectionString = "<connection string>"; //#4: Set connection string
        sqlCommand1.Connection = sqlConnection1;

        sqlConnection1.Open();

        sqlCommand1.CommandText = "SELECT id,parola FROM ANGAJAT WHERE id=@user AND parola=@pass";

        sqlCommand1.Parameters.AddWithValue("@user",textBox1.Text);
        sqlCommand1.Parameters.AddWithValue("@pass",textBox2.Text);

        using(SqlReader reader = sqlCommand1.ExecuteReader()) //#4 : Make use of `using` for object cleanup after use
        {
            table.Load(reader);  
        }

        sqlConnection1.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

按照Jon Skeet的观察结果改进asnwer的汇总更正:)