Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello please let me know how to make connection with ADO.NET using C#.please give me the answer as soon as possible.

Thanks and Regards
Alok kumar
Posted

Did you try
http://connectionstrings.com/[^]

It is the best resource of connectionstrings ever.

You just put your connectionstring in your config and open a new connection using it.
 
Share this answer
 
Comments
Abhinav S 12-Sep-10 12:27pm    
Good link!
Abhishek Sur 12-Sep-10 12:36pm    
It is the same link which I provided for say 100 times in codeproject .. . :)
In addition to the first answer, you can check out http://www.aspnet101.com/2008/01/a-beginners-guide-to-the-connection-string/[^].
 
Share this answer
 
Comments
Abhishek Sur 12-Sep-10 12:37pm    
Good to see this too... Cheers.
put this class on a .cs file in your project. Add a new item (Service Database) which will create a .mdf file (the database file for MSSQL). Double click on the file and open server explorer (view menu). Open the properties tab, and get the ConnectionString property. Replace the connectString field in my DataHelperMS class with the connection string. Please double the backslashes to avoid a compiler error.

In your event handler, just create a new DataHelperMS class and call its ExecuteDataSet / ExecuteNonQuery Methods. It should work perfectly
public class DataHelperMS
    {
        public static String connectString = "Data Source=.;AttachDbFilename=\"C:\\Users\\RR\\Documents\\Visual Studio 2008\\Projects\\PhilNits Exam Simulator\\PhilNits Exam Simulator\\exam.mdf\";Integrated Security=True;User Instance=True";

        public String ConnectionString
        {
            get {
         //       String connection = ConfigurationManager.ConnectionStrings["DataConnectionString"].ConnectionString;
           //     String result = String.Format(connection , Environment.CurrentDirectory.Substring(0, Environment.CurrentDirectory.IndexOf("\\bin\\")+1 ) );
        //      result = connectString;
                return connectString;
             }
        }
        SqlConnection conn;
        public DataHelperMS()
        {
            String conString = ConnectionString;
            this.conn = new SqlConnection(conString);
        }
        
        public int ExecuteNonQuery(String sql)
        {
            int result = 0;
            try
            {
                conn.Open();
                SqlCommand command = conn.CreateCommand();
                command.CommandText = sql;
                result = command.ExecuteNonQuery();
            }
            catch (SqlException)
            {
                result = 0;
                throw;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
        public DataSet ExecuteDataSet(String sql)
        {
            // Ensure that the current culture is US-English 
            //Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            DataSet result = null;
            try
            {
                conn.Open();
                SqlCommand command = conn.CreateCommand();
                command.CommandText = sql;
                result = ConvertDataReaderToDataSet(command.ExecuteReader());
            }
            catch (SqlException ex)
            {
                ex.ToString();
                result = null;
                throw;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
        public static DataSet ConvertDataReaderToDataSet(SqlDataReader reader)
        {
            DataSet dataSet = new DataSet();
            do
            {
                // Create new data table
                DataTable schemaTable = reader.GetSchemaTable();
                DataTable dataTable = new DataTable();
                if (schemaTable != null)
                {
                    // A query returning records was executed
                    for (int i = 0; i < schemaTable.Rows.Count; i++)
                    {
                        DataRow dataRow = schemaTable.Rows[i];
                        // Create a column name that is unique in the data table
                        string columnName = (string)dataRow["ColumnName"]; //+ "<C" + i + "/>";
                        // Add the column definition to the data table
                        DataColumn column = new DataColumn(columnName, (Type)dataRow["DataType"]);
                        try
                        {
                            dataTable.Columns.Add(column);
                        }
                        catch (DuplicateNameException)
                        {
                            int count = 0;
                            while (dataTable.Columns[columnName] != null)
                            {
                                columnName += count;
                                count++;
                            }
                            column.ColumnName = columnName;
                            dataTable.Columns.Add(column);
                        }
                    }
                    dataSet.Tables.Add(dataTable);
                    // Fill the data table we just created
                    while (reader.Read())
                    {
                        DataRow dataRow = dataTable.NewRow();
                        for (int i = 0; i < reader.FieldCount; i++)
                            dataRow[i] = reader.GetValue(i);
                        dataTable.Rows.Add(dataRow);
                    }
                }
                else
                {
                    // No records were returned
                    DataColumn column = new DataColumn("RowsAffected");
                    dataTable.Columns.Add(column);
                    dataSet.Tables.Add(dataTable);
                    DataRow dataRow = dataTable.NewRow();
                    dataRow[0] = reader.RecordsAffected;
                    dataTable.Rows.Add(dataRow);
                }
            }
            while (reader.NextResult());
            foreach (DataTable dt in dataSet.Tables)
            {
                dt.AcceptChanges();
            }
            return dataSet;
        }
        

    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900