Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I have a problem with connection to my sql database. I used a code from this article: Beginners guide to accessing SQL Server through C#[^].

When im clicking the button to connect, then compilator is showing me an error like: "
An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: invalid key value „integrated security
”."

Im trying it first time, and i rly dont know how to fix it.

This is my code, that is under button:
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection myConnection = new SqlConnection("user id=root;" + "password=;" + "server=localhost;" + "Trusted_Connection=no" + "database=drzewo;" + "connection timeout=30");
            try
            {
                myConnection.Open();
                
            }
            catch (System.Data.SqlClient.SqlException)
            {
                MessageBox.Show("Nie połączono!", "Error!");
            }
        }
Posted

Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.
 
Share this answer
 
Comments
Member 12146841 17-Nov-15 11:40am    
Ok, i did it. I just copied "connection string" to my SqlConnection, but what i need, to have a successful connection? My exception handling is showing me my messagebox.
OriginalGriff 17-Nov-15 11:52am    
So show the new current code, and the exact error message you get with it.
Member 12146841 17-Nov-15 12:14pm    
I think it's almost done, but the question is - What should i write in ApplicationIntent?

CODE:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection();
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "(localdb)/MSSQLLocalDB";
builder.InitialCatalog = "drzewo";
builder.IntegratedSecurity = true;
builder.ConnectTimeout = 15;
builder.Encrypt = false;
builder.TrustServerCertificate = false;
builder.ApplicationIntent = ???
builder.MultiSubnetFailover = false;
myConnection.ConnectionString = builder.ConnectionString;

try
{
myConnection.Open();
MessageBox.Show("Połączono z bazą danych!", "Connected!");

}
catch (System.Data.SqlClient.SqlException)
{
MessageBox.Show("Nie połączono!", "Error!");
}
}

Compilator is "saying" that i need to have ApplicationIntent = ReadWrite;, but it shows me an error.
OriginalGriff 17-Nov-15 12:22pm    
Comment that out, and just put the connection string you got from VS:

SqlConnection myConnection = new SqlConnection(@"The string you copied from the properties");

You know that string works, so use it! :laugh:
Member 12146841 17-Nov-15 12:35pm    
private void button1_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("Data Source = (localdb)\MSSQLLocalDB;Initial Catalog = drzewo; Integrated Security = True; Connect Timeout = 15; Encrypt = False; TrustServerCertificate = False; ApplicationIntent = ReadWrite; MultiSubnetFailover = False");


try
{
myConnection.Open();
MessageBox.Show("Połączono z bazą danych!", "Connected!");

}
catch (System.Data.SqlClient.SqlException)
{
MessageBox.Show("Nie połączono!", "Error!");
}
}

It doesnt want to work ^^ It shows me an error in place of the slash in data source. (Unrecognized Escape Sequence) ^^
Use a SqlConnectionStringBuilder to create your connectionstring.
 
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