我创建了本地SQL Server数据库和“登录”表单,这是“登录”表单的代码:
namespace WindowsFormsApplication1
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!(Usertxt.Text == string.Empty))
{
SqlConnection connection = new SqlConnection(@"Data Source=|DataDirectory|\crimemanagement.sdf");
connection.Open();
SqlCommand cmd = new SqlCommand(@"SELECT Count(*) FROM Login_table
WHERE Username=@Username and
Password=@Password", connection);
cmd.Parameters.AddWithValue("@Username", Usertxt.Text);
cmd.Parameters.AddWithValue("@Password", Passtxt.Text);
int result = (int)cmd.ExecuteScalar();
if (result > 0)
{
MessageBox.Show("Login Success");
}
else
{
MessageBox.Show("Incorrect login");
}
}
else if (Passtxt.Text == string.Empty || Usertxt.Text == string.Empty)
{
MessageBox.Show("Enter Username and Password");
}
}
}
}
但是,当我尝试使用创建的表单登录时,会出现连接错误并突出显示 connection.open();
未处理System.Data.SqlClient.SqlException
建立与SQL Server的连接时,发生了与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。(提供者:SQL网络接口,错误:26-指定服务器/实例时出错)
为了在ADO.NET中使用SQL Server Compact数据库文件,您需要使用System.Data.SqlServerCe.dll ADO.NET提供程序,并使用SqlCeConnection,SqlCeCommand等对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句