我已经尝试了一百万种不同的方法来实现这一目标,却发现没有任何方法可行。向下滚动到底部。 (注意,这是使用SQL Server数据库的登录表单)
DatabaseConnection.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AllIn1Database
{
class DatabaseConnection
{
private string sql_string;
private string strCon;
System.Data.SqlClient.SqlDataAdapter da_1;
public string Sql
{
set
{
sql_string = value;
}
}
public string connection_string
{
set { strCon = value; }
}
public System.Data.DataSet GetConnection
{
get { return MyDataSet(); }
}
private System.Data.DataSet MyDataSet()
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
con.Open();
da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
System.Data.DataSet dat_set = new System.Data.DataSet();
da_1.Fill(dat_set, "Table_Data_1");
con.Close();
return dat_set;
}
}
}
Form1.cs中:
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;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace AllIn1Database
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DatabaseConnection objConnect;
string conString;
DataSet ds;
SqlConnection con = new SqlConnection();
private void tnLogin_Click(object sender, EventArgs e)
{
try{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
con.Open();
String cmp = "SELECT ISNULL(stUsername, '') AS stUsername, ISNULL(stPassword,'') AS stPassword, ISNULL(stRole,'') AS stRole FROM LoginDetails WHERE stUsername='" + txtUname.Text + "' and stPassword='" + txtPass.Text + "'";
SqlCommand cmd = new SqlCommand(cmp, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string userText = txtUname.Text;
string passText = txtPass.Text;
if (this.CompareStrings(dr["Name"].ToString(), userText) &&
this.CompareStrings(dr["Password"].ToString(), passText)){
MessageBox.Show("Logged on as" + txtUname.Text);
}
else
{
MessageBox.Show("Invalid Logon Details.");
}
dr.Close();
con.Close();
}
}
catch (Exception err) {
MessageBox.Show(err.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
objConnect = new DatabaseConnection();
conString = Properties.Settings.Default.DatabaseConnectionString;
objConnect.connection_string = conString;
objConnect.Sql = Properties.Settings.Default.SQL;
ds = objConnect.GetConnection;
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
private bool CompareStrings(string string1, string string2)
{
return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
}
}
}
错误发生在
行中System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
错误说
strCon在此上下文中不存在。
如何在不抛出此错误的情况下成功连接?
答案 0 :(得分:1)
您正在实例化没有连接字符串的连接。而且我没有看到你在行之后设置它:
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
假设您的连接字符串位于Properties.Settings.Default.DatabaseConnectionString中,您可以执行以下操作:
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Properties.Settings.Default.DatabaseConnectionString);
答案 1 :(得分:0)
你没有连接字符串/没有给strCon一个值,你需要做类似的事情:
strCon = "myConnectionString";
找出连接字符串的用途,可以使用本网站: