【问题标题】:Pass arguments from C# objects to SQL [duplicate]将参数从 C# 对象传递到 SQL [重复]
【发布时间】:2013-06-01 21:20:38
【问题描述】:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            sqlConnection1.Open();
            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)";
            if (sqlCommand1.ExecuteNonQuery()!=null)
                MessageBox.Show("ESTE");
            sqlConnection1.Close();
        }
    }
}

我希望完成的是建立一个功能登录。我有一个数据库,一个名为“angajati”(员工)的表,其中包含 id(实际上是用户名)和 parola(密码)。我想知道的是,我可以在 SQL 脚本文本中引入“变量”,让它像函数一样运行,并简单地从我的文本框中传递值。这是我从其他示例中得出的,但它似乎不起作用。谁能给我一些关于这件事的见解?

【问题讨论】:

标签: c# sql-server visual-studio-2012


【解决方案1】:
private void button1_Click(object sender, EventArgs e)
        {
            sqlConnection1.Open();
            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'";
            if (sqlCommand1.ExecuteNonQuery()!=null)
                MessageBox.Show("ESTE");
            sqlConnection1.Close();
        }

【讨论】:

  • SQL 中的参数不需要引号。
猜你喜欢
  • 2013-08-09
  • 2017-12-03
  • 2015-02-24
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多