Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

An attempt to attach an auto-named database for file in WPF

$
0
0

Using : VS2010/ 2012 , Sql Server 2008 R2 ,ADO.NET ,Winformapp /WPF app

Problem :I am unable to connect to SQL Server local Database file (.mdf) through  SQL Server Authentication.

Requirment : by entering username and password in two text boxes after clicking on login button application has to validate credentials and display message box saying that "successfully login" , if failss "Login faild"

Description : created a MDF file using C# by clicking on Solution explorer and created a Table while connection this table showing following error:

  "

An attempt to attach an auto-named database for file E:\WPF Code\DBDemoRaja_2\DataBaseRajaWPF failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. "

Note : my sqlserver loging authentication username :sa, pwd :123

Code snipet:

please help me where iam making mistack

Regards

Srilatha

please help me any one

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string cs = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\WPF Code\DBDemoRaja_2\DataBaseRajaWPF;Integrated Security=True";
            SqlConnection cn = new SqlConnection(cs);
            string sql = string.Format("select   count(*) from MyUsers where UserName = '{0}' and PWD='{1}'", txtUsername.Text, txtPassword.Password);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            if (cn.State == ConnectionState.Closed)
                cn.Open();
            int cnt = int.Parse(cmd.ExecuteScalar().ToString());
            cn.Close();
            if (cnt > 0)
                MessageBox.Show("Valid User...", "Success");
            else
                MessageBox.Show("Invalid Username or password..", "Error");
<connectionStrings><add name="DBDemoRaja_2.Properties.Settings.DataBaseRajaWPFConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DataBaseRajaWPF.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" /></connectionStrings>

Please help me any one

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>