0
1.	protected void Button1_Click(object sender, EventArgs e)
2.	{string s="Data Source=Black-PC\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True";
3.	SqlConnection Conn = new SqlConnection(s);
4.	
5.	Conn.Open();
6.	string strqry = “Insert into students values (” + TextBox1.Text +
7.	“,’” + TextBox2.Text + “‘,’” + TextBox3.Text + “‘)”;
8.	 
9.	SqlCommand Com = new SqlCommand(strqry, Conn);
10.	Com.ExecuteNonQuery();
11.	Conn.Close();
12.	 
13.	}

when i create another page i have to rewrite connection string again, can anybody explain how to avoid writing connection string variable again and again, plz help me,

4
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by __avd
0

Thanks adatapost. are there any video tut to learn sql database connection with C#

0

/*The best way of creating connetion is using a separate class for connection.. */

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace ImageUpload_Download
{
    class connection_class
    {

        public String connection_string = "Data Source=MY_PC;integrated security=true;initial catalog=master";
        public SqlConnection conn;
        public connection_class()
        {
            //default constructor here
        }
        public bool create_connection()
        {
            try
            {
                conn = new SqlConnection(connection_string);
                conn.Open();
                return true;
            }
            catch (Exception a)
            {
                System.Console.Write(a.ToString());
                return false;
            }
        }

        public bool delete_connection()
        {
            try
            {
                conn.Close();
                return true;
            }
            catch (Exception c)
            {
                System.Console.Write(c.ToString());
                return false;
            }
        }



    }

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

0

/*The best way of creating connetion is using a separate class for connection.. */

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace ImageUpload_Download
{
    class connection_class
    {

        public String connection_string = "Data Source=MY_PC;integrated security=true;initial catalog=master";
        public SqlConnection conn;
        public connection_class()
        {
            //default constructor here
        }
        public bool create_connection()
        {
            try
            {
                conn = new SqlConnection(connection_string);
                conn.Open();
                return true;
            }
            catch (Exception a)
            {
                System.Console.Write(a.ToString());
                return false;
            }
        }

        public bool delete_connection()
        {
            try
            {
                conn.Close();
                return true;
            }
            catch (Exception c)
            {
                System.Console.Write(c.ToString());
                return false;
            }
        }



    }

Thnanks a lot ,i m new to asp.net and C# how can i use this class when i inserting data, in normal way i pass Sqlconnecton variable and string variable(where i insert data)to SqlCommand, then close connection ,plzzzzzzzz
explain with me a sample code

Edited by MrBlack: n/a

0

Thnanks a lot ,i m new to asp.net and C# how can i use this class when i inserting data, in normal way i pass Sqlconnecton variable and string variable(where i insert data)to SqlCommand, then close connection ,plzzzzzzzz
explain with me a sample code

you just need to create properties in connection class and use this properties in your code wherever you want. you can define property in below way in your connection class.

public SqlConnection dbCon = new SqlConnection("your connection string or retrieve it from config file");

public SqlConnection dbConString
{
get { return dbCon; }
}

0

you just need to create properties in connection class and use this properties in your code wherever you want. you can define property in below way in your connection class.

public SqlConnection dbCon = new SqlConnection("your connection string or retrieve it from config file");

public SqlConnection dbConString
{
get { return dbCon; }
}

Thanks a lot

0

Please mark this thread as solved if you have found an answer to your question and good luck!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.