Reputation: 65
I've created a local SQL Server database and Login form, Here is the code of Login form :
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");
}
}
}
}
But when I try to login using the form which I created it give me a connection error and highlights connection.open();
System.Data.SqlClient.SqlException was unhandled
A network-related or instance-specific error occurred while establishing a >connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Upvotes: 0
Views: 152
Reputation: 755321
You're using SqlConnection
which is for proper SQL Server (the server-based system), but you're referencing a SQL Server CE data file (.sdf
).
If you want to use the .sdf
, you need to use SqlCeConnection
, SqlCeCommand
etc. - not the SqlConnection
and SqlCommand
Upvotes: 0
Reputation: 41819
In order to work with SQL Server Compact database files in ADO.NET you need to use the System.Data.SqlServerCe.dll ADO.NET provider, and use objects like SqlCeConnection, SqlCeCommand etc.
Upvotes: 2