Who is connected to database - Sample Application C#
This is a sample application just to show who is currently connected into database MSSQL. The application actually just execute MSSQL command "sp_who" and show the data into datagrid.
Below is a screen shoot of the sample application.
By Mohd Zulkamal
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
Below is a screen shoot of the sample application.
Code Behind View Who Is Connected Form
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string ipaddress = textBox1.Text;
string username = textBox2.Text;
string password = textBox3.Text;
string dbNAme = textBox4.Text;
ViewUserActive form = new ViewUserActive();
form.popupInformation(ipaddress, username, password, dbNAme);
}
private void Form1_Load(object sender, EventArgs e)
{
}
Code Behind ViewUserActive Form
string ipaddress;
public string Ipaddress
{
get { return ipaddress; }
set { ipaddress = value; }
}
string username;
public string Username
{
get { return username; }
set { username = value; }
}
string password;
public string Password
{
get { return password; }
set { password = value; }
}
string dbNAme;
public string DbNAme
{
get { return dbNAme; }
set { dbNAme = value; }
}
public ViewUserActive()
{
InitializeComponent();
}
public void popupInformation(string ipadd, string usrName, string pass, string databaseName)
{
ViewUserActive form = new ViewUserActive();
form.Ipaddress = ipadd;
form.Username = usrName;
form.Password = pass;
form.DbNAme = databaseName;
form.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void ViewUserActive_Load(object sender, EventArgs e)
{
string strConnection = "Data Source=" + Ipaddress + ";Initial Catalog=" + DbNAme + ";Persist Security Info=True;User ID=" + Username + ";Password=" + Password;
SqlConnection dbConn = new SqlConnection(strConnection);
SqlCommand dbComm;
SqlDataReader dbReader;
DataTable table = new DataTable();
table.Columns.Add("SPID", typeof(string));
table.Columns.Add("ECID", typeof(string));
table.Columns.Add("STATUS", typeof(string));
table.Columns.Add("LOGINNAME", typeof(string));
table.Columns.Add("HOSTNAME", typeof(string));
table.Columns.Add("BLK", typeof(string));
table.Columns.Add("DBNAME", typeof(string));
table.Columns.Add("CMD", typeof(string));
table.Columns.Add("REQUESTID", typeof(string));
DataTable _temp = new DataTable();
string strsql = "sp_who";
dbComm = new SqlCommand(strsql, dbConn);
dbConn.Open();
dbReader = dbComm.ExecuteReader();
if (dbReader.HasRows)
{
while (dbReader.Read())
{
string dbName = dbReader["dbname"].ToString();
if (dbName.Trim().ToUpper() == DbNAme.Trim().ToUpper())
{
DataRow row = table.NewRow();
row["SPID"] = dbReader["spid"].ToString();
row["ECID"] = dbReader["ecid"].ToString();
row["STATUS"] = dbReader["status"].ToString();
row["LOGINNAME"] = dbReader["loginame"].ToString();
row["HOSTNAME"] = dbReader["hostname"].ToString();
row["BLK"] = dbReader["blk"].ToString();
row["DBNAME"] = dbName;
row["CMD"] = dbReader["cmd"].ToString();
row["REQUESTID"] = dbReader["request_id"].ToString();
table.Rows.Add(row);
}
}
}
dbReader.Close();
dbConn.Close();
dataGridView1.DataSource = table;
}
By Mohd Zulkamal
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)