using System;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
private const string ConnectionString = "Your_Connection_String_Here";
public Form1()
{
InitializeComponent();
}
private void AuthenticateButton_Click(object sender, EventArgs e)
{
string login = textBox1.Text;
string password = textBox2.Text;
string role = CheckCredentials(login, password);
MessageBox.Show(role ?? "Неверные учетные данные");
}
private string CheckCredentials(string login, string password)
{
const string query = "SELECT role FROM Users WHERE login = @login AND password = @password";
using (var connection = new SqlConnection(ConnectionString))
using (var cmd = new SqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@login", login);
cmd.Parameters.AddWithValue("@password", password);
connection.Open();
return (string)cmd.ExecuteScalar();
}
}
}
}
Ошибка CS0119 "ConnectionString" является тип, который недопустим в данном контексте.
using MongoDB.Driver.Core.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows .Forms;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string login = textBox1.Text;
string password = textBox2.Text;
string role = Check(login, password);
if (role == "Admin")
MessageBox.Show("Admin");
else if (role == "Master")
MessageBox.Show("Master");
else if(role == "User")
MessageBox.Show("User");
else if (role == "Nothing")
MessageBox.Show("Nothing");
}
private string Check(string login, string password)
{
SqlConnection connection = new SqlConnection(ConnectionString);
string query = "SELECT role FROM {Table} WHERE (login = @login AND password)";
SqlCommand cmd = new SqlCommand(query, connection);
connection.Open ();
cmd.Parameters.AddWithValue("login", login);
cmd.Parameters.AddWithValue("password", password);
SqlDataReader reader = cmd.ExecuteReader();
if ( reader.Read ())
{
return reader["role"].ToString();
}
else return null;
}
}
}
Как исправить ошибку?