C# Exercises

Home AgriMetSoft About Contact

SQL Connection String in | C#

	    	
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SQL_Connection_String
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void connect_Click(object sender, EventArgs e)
    {
      string connetionString;
      System.Data.SqlClient.SqlConnection cnn;
      connetionString = @"Data Source=DESKTOP-J9K51EL\AMSSQL;Initial Catalog=Database1;Integrated Security=True";
      cnn = new System.Data.SqlClient.SqlConnection(connetionString);
      string query = "select * from Table1";
      System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query, cnn);
      cnn.Open();
      // create data adapter
      System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
      DataTable dataTable = new DataTable();
      // this will query your database and return the result to your datatable
      da.Fill(dataTable);
      MessageBox.Show("Connection Open !");
      dataGridView1.DataSource= dataTable;
      cnn.Close();
    }
  }
}

		
	 


Download the project of Visual Studio 2013 in DropBox Download


C# SQL DataBase Connection String get Table to DataGridView


List of Exercises