I made a MySQL server database file from server explorer using the following code to connect the MySQL database:
private void DataAdd_Load(object sender, EventArgs e)
{
try
{
var conn = new MySqlConnection();
conn.ConnectionString =
"Data Source=(LocalDB)\\MSSQLLocalDB;" +
"User Instance=true;" +
"Integrated Security=false;" +
"AttachDbFilename=C:\\Path\\filename.MDF;";
conn.Open();
MessageBox.Show("Connected to database");
}
catch (Exception e1)
{
MessageBox.Show("Connection failed");
}
}
But the connection always fails.
The error that I found while debugging:
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll ("The user instance login flag is not allowed when connecting to a user instance of SQL Server. The connection will be closed.")
To connect to MySQL, you need MySqlConnection and a proper MySQL connection string:
private void DataAdd_Load(object sender, EventArgs e)
{
try
{
var conn = new MySqlConnection(#"Server=192.168.1.10;Database=myDB;Uid=myUsername;Pwd=myPassword;");
conn.Open();
MessageBox.Show("Connected to database");
}
catch (Exception e1)
{
MessageBox.Show("Connection failed");
}
}
You will need to use MySQLConnection as answered here:
ASP.NET use SqlConnection connect MySQL
The MySQL connection library might not be included in your solution, so you will need to download it. And change var conn = new SqlConnection(); to:
var conn = new MySqlConnection();
Related
I'm trying to connect my windows form app with my embedded database (.dbf) and I keep getting this message no matter what I do to the connection string:
error isam instalable cant be found
Here is the code I'm using to test the whole thing:
private void bGuardar_Click(object sender, EventArgs e)
{
try
{
string cadena = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =D:\\; Extended Properties = dBASE IV; UserID =; Password =;";
OleDbConnection con = new OleDbConnection();
con.ConnectionString = cadena;
con.Open();
MessageBox.Show("conected");
con.Close();
}
catch (OleDbException exp)
{
MessageBox.Show("Error: " + exp.Message);
}
}
I think the problem is in the connection string.
The database I'm trying to connect to is on an AWS server and I'm trying to write a C# script that will pull data from the database. The only tutorials I can find are using localhost. Here's some code I tried:
static void Main()
{
SqlConnection myConnection = new SqlConnection(
"user id=MyUsername;" +
"password=MyPassword;" +
"server=MyServerName.ctf1qojvktpk.us-west-2.rds.amazonaws.com:3306;" +
"Trusted_Connection=yes;" +
"database=MyDBName; " +
"connection timeout=30");
try
{
myConnection.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
It looks like it's a mySql database. You need to install the MySQL Connector for .NET.
Example connectionstring
<add name="MyConnection" connectionString="server=myamazonserver.eu-central-1.rds.amazonaws.com;user id=rootusername;password=mypassword;database=mydatabasename; Convert Zero DateTime=True; Allow User Variables=True" providerName="MySql.Data.MySqlClient" />
Or in plain code
static void Main()
{
SqlConnection myConnection =
new SqlConnection(
"server=myamazonserver.eu-central-1.rds.amazonaws.com;user id=rootusername;password=mypassword;database=mydatabasename; Convert Zero DateTime=True; Allow User Variables=True" providerName="MySql.Data.MySqlClient"
);
try
{
myConnection.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
I am trying to connect the MS Access Database from the server and finding no luck.
I see the below image("Insert Operation Error") message while trying to save the information.
Can anyone please help? What went wrong in the below code?
Insert Operation Error
protected void btnsave_Click(object sender, EventArgs e)
{
string constring = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("DB\\Contact.DB");
string SqlString = "Insert Into BUREAUXDETUDES (mail1,mail2,tel1,tel2) Values (#mail1,#mail2,#tel1,#tel2)";
OleDbConnection con = new OleDbConnection(constring);
try
{
OleDbCommand cmd = new OleDbCommand(SqlString, con);
con.Open();
cmd.Parameters.AddWithValue("#mail1", txtemail1.Text);
cmd.Parameters.AddWithValue("#mail2", txtemail2.Text);
cmd.Parameters.AddWithValue("#tel1", txttel1.Text);
cmd.Parameters.AddWithValue("#tel2", txttel2.Text);
cmd.ExecuteNonQuery();
lblmessage.Text = "Your Information Saved Successfully";
}
catch (Exception emsg)
{
lblmessage.Text = emsg.Message;
}
finally
{
con.Close();
}
}
The Error is telling you that the location of your .db file is incorrect.
You can change the path to the file in this line.
string constring = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("DB\\Contact.DB");
You need to install Microsoft Excel engine first.
You can download it from the link bellow
https://www.microsoft.com/en-us/download/details.aspx?id=13255
I am trying to get connection with Oracle database but there is an error that shows that data source not found! Please can anyone help?
string ConnectionString= "Data Source=voldemort-pc;User ID=student;Password=***********;Unicode=True";
OdbcConnection conn = new OdbcConnection(ConnectionString);
try
{
conn.Open();
MessageBox.Show("success!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
iam writing a c# windows application to store some data into a sql server database , but when i make an attempt to attach my mdf database file to visual studio the above error appears here's the code below , what can i do ? Thanks.
public partial class Add_Client : Form
{
SqlConnection clientConnection;
string connString;
SqlCommand insertCommand;
public Add_Client()
{
InitializeComponent();
connString = "Data Source=ESLAM\\MSSQLSERVER;Initial Catalog=Clients; Integrated security=true ";
clientConnection = new SqlConnection();
clientConnection.ConnectionString = connString;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlCommand insertCommand = new SqlCommand();
insertCommand.Connection = clientConnection;
insertCommand.CommandText = "INSERT INTO Client_Info values(#Client_Name,#Autorization_No,#Issue_Type,#Status)";
insertCommand.Parameters.Add("#Client_Name", SqlDbType.NVarChar, 60).Value = txt_Name.Text;
insertCommand.Parameters.Add("#Autorization_No", SqlDbType.Int, 60).Value = txt_Auth.Text.ToString();
insertCommand.Parameters.Add("#Issue_Type", SqlDbType.Text, 200).Value = txt_Iss.Text;
insertCommand.Parameters.Add("#Status", SqlDbType.Text, 200).Value = txt_Iss.Text;
//insertCommand.Parameters.Add("#Date To Memorize", SqlDbType.Date, 15).Value=Ca_Mem.se;
insertCommand.Connection.Open();
insertCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (clientConnection != null)
{
clientConnection.Close();
}
}
}
}
Your connection string is just trying to connect to a running instance (.\INSTANCE2) of sql server with the DB (Client) already attached. The error is saying there is no sql server instance running with that name on the local machine. Nothing there will attach anything to anywhere.
Here are instructions on how to get an mdf file attached in sql server express. If you run sqlcmd as a postbuild task in visual studio and run the a modified version of the script for your db and it will start sqlserver and attach your database after you build.