Assumptions:
Server Name: MSSQLSERVER2008Database: project
Table: login
Data in Table:
id
|
username
|
password
|
1
|
pabitra
|
dangol
|
Form Design
Source Code
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data; //used for database connection
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; //used for database connection
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
string connectionString = null;
SqlConnection sqlCnn;
SqlCommand sqlCmd;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
int i = 0;
string sql = null;
//connecting SQL Server 2008 using Windows Authentication Mode via C Sharp
connectionString = "Data Source=.\\MSSQLSERVER2008; Initial Catalog=project;Integrated Security=SSPI";
sql = "Select * from login";
sqlCnn = new SqlConnection(connectionString);
try
{
sqlCnn.Open();
sqlCmd = new SqlCommand(sql, sqlCnn);
adapter.SelectCommand = sqlCmd;
adapter.Fill(ds);
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
txtid.Text = Convert.ToString(ds.Tables[0].Rows[i].ItemArray[0]);
txtusername.Text = Convert.ToString(ds.Tables[0].Rows[i].ItemArray[1]);
txtpassword.Text = Convert.ToString(ds.Tables[0].Rows[i].ItemArray[2]);
MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " - " + ds.Tables[0].Rows[i].ItemArray[1] + " - " + ds.Tables[0].Rows[i].ItemArray[2]);
}
adapter.Dispose();
sqlCmd.Dispose();
sqlCnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
}
}
No comments:
Post a Comment