Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
//In Appconfig....

 <add key="ClientConString" value="SERVER=localhost;User Id=root;pwd=mysql;database=lnt;Persist Security Info=True;Allow Zero Datetime=true"/>


//I have used these namespaces in my code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

//To call the connection code is...
 MySqlConnection clientCon = new MySqlConnection(ConfigurationManager.AppSettings["ClientConString"].ToString());


why the connection hasn't established ?
Posted
Updated 11-Sep-12 2:09am
v3
Comments
Rock (Multithreaded) 11-Sep-12 8:02am    
What is the problem in this code?
Kuthuparakkal 11-Sep-12 8:05am    
error plz
AmitGajjar 11-Sep-12 8:14am    
question is not complete, use Improve question to update your question.
[no name] 11-Sep-12 9:30am    
Your server is not the localhost, your connection string is not being read, or you are not calling the Open method on your connection.
bbirajdar 11-Sep-12 10:40am    
Use ******** as password... lol

Have a look here on how to connect DB & get data: Beginners guide to accessing SQL Server through C#[^]

Further info regarding the data communication can be read here: Accessing data with ADO.NET[^]
 
Share this answer
 
I suggest that you hardcode your connection string first instead of reading it from a config file to test that you can actually open a connection.

C#
MySqlConnection clientCon = new MySqlConnection("Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword");

clientCon.Open()


Play around with different values until you get a connection. Once you have the appropriate values, you can plug them in your app config.
 
Share this answer
 
XML
you can try this

in web config file.

<add name="ConnectionString" connectionString="Data Source=.;Initial Catalog=lnt;Persist Security Info=True;User ID=root;;Password=mysql" providerName="System.Data.SqlClient"/>


using System.Data.SqlClient;

 SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
 
Share this answer
 
also you can follow the path :
Tools/ConnectToDatabase and get complete connection string and use in your project .
 
Share this answer
 
First Create a create_connection class:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace dailyNotes
{
    class create_connection
    {
        public static SqlConnection CreateConnection()
        {
            string ConnString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Hizbul Bahar\Documents\dailynotes.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlConnection Conn = new SqlConnection(ConnString);
            Conn.Open();
            return Conn;
        }
        public void disconnect(SqlConnection conn)
        {
            conn.Close();
        }
    }
}

after that , when you want to insert, retrieve or update data you can use this class
many time. just do this.
C#
String query="";
               SqlConnection Conn = create_connection.CreateConnection();
               SqlCommand cmd = new SqlCommand(query, Conn);
               cmd.ExecuteNonQuery();
               Conn.Close();
 
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