Tuesday 8 April 2014

SQL server database connection in Window Form


                                                                                                                                          Next

I will start first Tutorial with SQL server database connection. Here i will explain step by step. follow these steps.
step (1):- create a window form
Open visual stdio and create a new project and select window form application and give it name as First_Csharp app.

step (2):- Drag and down a button
now from tool box drag and down a button and click on button

step (3):-Database table
create a database table in sql server. here database name is windowapp and tablename is data


step (4):-Coding on button click
first insert a namespace "using System.Data.SqlClient".
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

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

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
                String query = "select * from data";
                SqlConnection con = new SqlConnection(str);
                SqlCommand cmd = new SqlCommand(query, con);
                con.Open();
                DataSet ds = new DataSet();
                MessageBox.Show("connect with sql server");
                con.Close();
            }
            catch (Exception es)
            {
                MessageBox.Show(es.Message);

            }
        }
    }
} 
step (5):-Run the application 
                                                                                                                                          Next



No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...