Connection string in App.config file.
in App.Config file write down below coding.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="con" connectionString="Data Source=.;Initial Catalog=vdm;Integrated Security=True;user id=sa password=******"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
2. Create a new class file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace vidhyamandir
{
public class dbConnection
{
public string con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
}
}
3.I have a login page in this I created a connection see below
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 vidhyamandir
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
dbConnection db = new dbConnection();
SqlConnection con;
private void button1_Click(object sender, EventArgs e)
{
con = new SqlConnection(db.con);
SqlCommand cmd = new SqlCommand("select username , password from login where username='"+textBox1.Text+"' and password='"+textBox2.Text+"'",con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
frmmainmenu m = new frmmainmenu();
this.Hide();
m.Show();
}
con.Close();
}
}
}
in App.Config file write down below coding.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="con" connectionString="Data Source=.;Initial Catalog=vdm;Integrated Security=True;user id=sa password=******"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
2. Create a new class file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace vidhyamandir
{
public class dbConnection
{
public string con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
}
}
3.I have a login page in this I created a connection see below
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 vidhyamandir
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
dbConnection db = new dbConnection();
SqlConnection con;
private void button1_Click(object sender, EventArgs e)
{
con = new SqlConnection(db.con);
SqlCommand cmd = new SqlCommand("select username , password from login where username='"+textBox1.Text+"' and password='"+textBox2.Text+"'",con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
frmmainmenu m = new frmmainmenu();
this.Hide();
m.Show();
}
con.Close();
}
}
}
No comments:
Post a Comment