Im receiving a MySqlException was unhandled on a line: conn.Open();
MySqlConnection conn = new MySqlConnection("host=fdb5.freehostingeu.com;user=1477630_one;password=******;database=1477630_one;");
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE name = '"+name+"'AND surname = '"+ surname +"';");
cmd.Connection = conn;
conn.Open();
I think the format of my connection string is wrong. and I've tried altering the values to see whether it's the problem but I'm still unable to connect.
what am I doing wrong?
According to their live support, they do not offer Remote Connection to MySQL on the free packages.
ConnectionStrings.com shows this as the correct standard format:
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Adjusting yours accordingly:
Server=fdb5.freehostingeu.com;Uid=1477630_one;Pwd=******;database=1477630_one;
See this other answer if you need to allow remote connections to MySQL.
Shared web hosts almost never allow remote access to shared resources such as MySQL. What you will need to do is install a copy of MySQL in your local environment to do you development and testing. Then push your schema out through whatever tools they provide to you - these are usually web based. Then when you push your site to the shared host you the connection string you are using should work fine.
1) Be sure to add a reference to MySQL.Data
2) Include using MySql.Data.MySqlClient
3) Your connection string should be formatted as such:
connectionString = "SERVER=" + yourserver + ";" + "DATABASE=" + databasename + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
4) Do a try-catch statement to catch the errors
Related
So, in the tutorial I was checking, he uses a Database and connect it using this
SqlConnection conn = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\RANBAH~1\Documents\testlogin.mdf;Integrated Security=True;Connect Timeout=30");
Although, I use SQL Server Management 17, which mean I have a server, So how do i get my SQL Connect Data Source? because afterward he uses it for
SqlDataAdapter sda = new SqlDataAdapter("select count(*) from login where username ='" + textBox1.Text + "' and password='" + textBox2.Text + "'", conn);
Any clue ?
Inside your project, in VS, connect to the server. Once you are on the server explorer, right click on the database to get the connection string. When you check the properties for the database, you should be able to see it.
You cannot get the connection string from SSMS, though you can get all the information for the connection string. Then, you can use those information to create your own connection string using this website: https://www.connectionstrings.com/ Just check which is better suited for you.
try
{
//Create our connection strings
string sSqlConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath)) + "\\ClaimFiles.mdf;Integrated Security=True;User Instance=True";
MessageBox.Show(sSqlConnectionString);
//Execute a query to erase any previous data from our destination table
string sClearSQL = "DELETE FROM PA";
SqlConnection SqlConn = new SqlConnection(sSqlConnectionString);
SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlConn);
SqlConn.Open();
MessageBox.Show(SqlCmd.ExecuteNonQuery().ToString());
SqlConn.Close();
}
catch (SqlException ex)
{
//handle exception
StringBuilder errorMessages = new StringBuilder();
for (int i = 0; i < ex.Errors.Count; i++)
{
errorMessages.Append("Index #: " + i + "\n" +
"Message: " + ex.Errors[i].Message + "\n" +
"ErrorNumber: " + ex.Errors[i].Number + "\n" +
"Source: " + ex.Errors[i].Source + "\n" +
"Severity Level: " + ex.Errors[i].Class + "\n" +
"Server:" + ex.Errors[i].Server + "\n");
MessageBox.Show(errorMessages.ToString());
}
}
Above is my code in C#, i'm using Microsoft SQL express. the code above is activated upon a click. When i run the code in Visual Studio everything works fine. But when i copy the folder of the project to a different computer(OS: Windows XP) and run the .exe file the program catches a SqlException:
An error has occured while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error 26 - Error Locating Server/Instance Specified)
Can someone help me with this, it would be a great help to solve this problem, because the program must run in a different computer. By the way program's target framework is .NET 3.5
Make sure your server name is correct, e.g., no typo on the name.
Make sure your instance name is correct and there is actually such an instance on your target machine. [Update: Some application
converts \ to . If you are not sure about your application,
please try both Server\Instance and Server\Instance in your
connection string]
Make sure the server machine is reachable, e.g, DNS can be resolve correctly, you are able to ping the server (not always true).
Make sure SQL Browser service is running on the server. If firewall is enabled on the server, you need to put sqlbrowser.exe and/or UDP
port 1434 into exception.
This seems to be a good reference :
http://blogs.msdn.com/b/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx
I am create a database utility and I seem to not be able to get my connectionstring correct.
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=.\\SQLExpress;" +
"User Instance=true;" +
"Integrated Security=true;" +
"AttachDbFilename=|DataDirectory|ConfigurationData.mdf;";
I believe this is in the correct format. As for the data source, my sql server is SQLExpress which runs sql server 2008 R2. My database is named ConfigurationData. Am I missing something?
When I run it, it opens the database - I assume it does since it does not through exception - but when I try inserting into a table, it does not actually insert it yet it executes the command.
conn.Open();
try
{
SqlCommand comm = new SqlCommand("INSERT INTO Test " + "(id,number) " + " VALUES(" + 10 + " , " + 12 + ")", conn);
comm.ExecuteNonQuery();
Console.WriteLine("Database is created successfully", "MyProgram");
}
catch (Exception ex)
{
}
finally
{
if ((conn.State == ConnectionState.Open))
{
conn.Close();
}
}
EDIT
Just remembered that I had answered a similar question a while back. Check it out:
Why can't I insert data into local database (SQL Compact Edition) with C#?
I don't think it is the connection string issue. But for your reference, a good site to refer to is http://www.connectionstrings.com/sql-server-2008/
You would need one of these:
Attach a database file, located in the data directory, on connect to a local SQL Server Express instance
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;
Trusted_Connection=Yes;
Attach a database file on connect to a local SQL Server Express instance
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;
Using an User Instance on a local SQL Server Express instance
The User Instance functionality creates a new SQL Server instance on the fly during connect. This works only on a local SQL Server instance and only when connecting using windows authentication over local named pipes. The purpose is to be able to create a full rights SQL
Server instance to a user with limited administrative rights on the computer.
Data Source=.\SQLExpress;Integrated Security=true;
AttachDbFilename=C:\MyFolder\MyDataFile.mdf;User Instance=true;
To use the User Instance functionality you need to enable it on the SQL Server. This is done by executing the following command: sp_configure 'user instances enabled', '1'. To disable the functionality execute sp_configure 'user instances enabled', '0'.
try (local) instead of dot, dot is not recognized in Win XP
conn.ConnectionString =
"Data Source=(local)\\SQLExpress;" +
"User Instance=true;" +
"Integrated Security=true;" +
"AttachDbFilename=|DataDirectory|ConfigurationData.mdf;";
You're SQL Statement isn't right also, and you should use parameters, but here is what you should have
SqlCommand comm = new SqlCommand("INSERT INTO Test (id, number) VALUES('" + 10 + " ', '" + 12 + "')", conn);
Why not just use the SqlConnectionStringBuilder class?:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = #"(local)\SQLExpress";
builder.UserInstance = true;
builder.IntegratedSecurity = true;
builder.AttachDBFilename = "|DataDirectory|ConfigurationData.mdf";
SqlConnection conn = new SqlConnection(builder.ConnectionString());
The output:
"Data Source=(local)\\SQLExpress;AttachDbFilename=|DataDirectory|ConfigurationData.mdf;Integrated Security=True;User Instance=True"
One of the way to do this is to add your connection string in web.config file as shown below:
Jus click on the properties of database on the database explorer. There you will find connectionstring in its properties. Jus add it in connectionstring below.
<configuration>
<connectionStrings>
<add name="ConnectionName" connectionString="your connection string" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Then in the page you can store it in string or directly refer to your connection as shown below:
Connection con=new SqlConnection();
con.ConnectionString=ConfigurationManager.ConnectionStrings["connString"].ToString();
and I suspect your insert statement is not properly declared.
Just try this:
SqlCommand comm = new SqlCommand("INSERT INTO Test (id,number) VALUES('10' ,'12')", con);
That's all from my part... Hope it helped you..
I use the code bellow to connect to my database on my website:
string tempstr = "Data Source=" + "72.55.---.---" + ";Initial Catalog=-------;Integrated Security=False;Persist Security Info=True;User ID=" + "MYUSER" + ";Password=" + "MYPASS";
SqlConnection con = new SqlConnection(tempstr);
con.Open();
Is it secure enough ? or someone who is not professional hacker can capture the username and the password which is sent from his/her computer to my database !?
thanks in advance.
Strings like the above will be stored in plain text in the executable.
If the "attacker" has access to the DLL, it is a simple thing to dump all the strings contained in it - including the connection string.
If you want to secure the connection string, a common practice is to store it in the connectionStrings section of the application .config file and encrypt it.
I am having a table with identifier GUID in mysql (binary 16).
To execute CRUD operations i use EF driver that comes with the installation of mysql connector v.6.3.6.
When trying to insert a new object through EF it fails with error {"Data too long for column 'MyIdentifierColumnId' at row 1"}
MyIdentifierColumnId is binary 16 and translated as GUID in .NET. So the length should not be an issue.
Any hints ?
According to the http://dev.mysql.com/doc/refman/5.1/en/connector-net-connection-options.html
Columns defined as binary(16) or char(36) are treated as old guids. In the new version UUID are introduced and if we want to use Old Guids=true in the connection string.
Your answer was very helpful, thanks. But I had problems by adding this "Old Guids=true" I was not shore how and where to add it, my connection was failing. So it might be helpful for others to have this piece of code:
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";Old Guids=true;";
connection = new MySqlConnection(connectionString);