how to connect MYSQL server using ASP.NET? - asp.net

I want to use MYSQL server with my asp.net application. But i am not able to connect to it. I am getting error that is "ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified " .
My code is:-
System.Data.Odbc.OdbcConnection cn = new System.Data.Odbc.OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=new_testdb; User=root;Password=abc123#;");
cn.Open();
System.Data.Odbc.OdbcCommand cmd = new System.Data.Odbc.OdbcCommand();
System.Data.Odbc.OdbcDataAdapter adp = null;
DataSet ds = new DataSet();
cmd.Connection = cn;
cmd.CommandText = "Select * from new_table";
adp = new System.Data.Odbc.OdbcDataAdapter(cmd);
adp.Fill(ds, "new_table");
this.GridView1.DataSource = ds;
this.GridView1.DataMember = "new_table";
cn.Close();

Try to download ADO.NET MySql Connector API (managed MySql Data provider) instead of ODBC Driver.
EDIT: Connector/NET Examples

You also may connect to MySQL with dotConnect for MySQL components.
Try to build the connection string with MySqlConnectionStringBuilder class.

This is what you need: http://dev.mysql.com/downloads/connector/net
Enjoy!

Could you please see here My SQL ConnectionString or ConnectionString

Click Left btn on database in server explorer, select properties copy connection string;
2.string c= persistsecurityinfo=True;server=localhost;user id=root;password=admin;database=sam;
MySqlConnection cn = new MySqlConnection(c);
cn.Open();
Response.Write("Connection successful !!");
MySqlDataAdapter Mda = new MySqlDataAdapter("select * from tblName", cn);
DataSet ds = new DataSet();
Mda.Fill(ds, "tblName");
GridView1.DataSource = ds.Tables["tblName"];
GridView1.DataBind();
Make sure that :-
using System.Data;
using MySql.Data.MySqlClient;
imported... :)

Related

Post Error when submitting Angular form data to Azure SQL Database

I have a fully working ASP.NET Angular website that saves a form data to a database. I have managed to deploy it to Azure App Service and also connected to Azure SQL Database using visual studio. However when I save the form I have a post error. This is my first time, is there anything else that I need to do in order for the form to work because initially it was working with my local database?.
This is a screenshot of the error
I am creating an Azure SQL database and inserting data from asp.net. Connection_time_out error comes when the application is not connected to the server.
Create Azure SQL Database and create the table.
C# code for insert data into Azure Sql Database.
static void Main(string[] args)
{
//Connection String
string cs = "Server=tcp:xxx.database.windows.net,1433;Initial Catalog=punitdb;Persist Security Info=False;User ID=xxx;Password=xxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
SqlConnection con;
SqlDataAdapter adapt;
DataTable dt;
con = new SqlConnection(cs);
con.Open();
SqlCommand cmd;
string s = "insert into Persons values(#PersonID,#LastName,#FirstName,#email,#message)";
cmd = new SqlCommand(s, con);
cmd.Parameters.AddWithValue("#PersonID", 1);
cmd.Parameters.AddWithValue("#LastName", "Kumar");
cmd.Parameters.AddWithValue("#FirstName", "Roshan");
cmd.Parameters.AddWithValue("#email", "Roshan#gmail.com");
cmd.Parameters.AddWithValue("#message", "Hello Roshan");
cmd.CommandType = CommandType.Text;
int i = cmd.ExecuteNonQuery();
con.Close();
Console.WriteLine("Press Any key for Show data ");
con = new SqlConnection(cs);
con.Open();
adapt = new SqlDataAdapter("select top 1 * from Persons order by PersonID desc", con);
dt = new DataTable();
adapt.Fill(dt);
Console.WriteLine("First Name :-" + dt.Rows[0][2] + ", Last Name :-" + dt.Rows[0][1] + ", Emamil :-" + dt.Rows[0][3] + ", Message :- " + dt.Rows[0][4]);
Console.ReadKey();
}
code output
After running code
Microsoft document on Connection timeout expired errors
Read this Microsoft document for Common connection issue steps to fix common connection issues

System.Data.SQLite : VACUUM INTO an in-memory database

First I use System.Data.SQLite v1.0.111 to open a connection to in-memory database as below.
SQLiteConnection dest= new SQLiteConnection("FullUri='file:db1?mode=memory&cache=shared'");
dest.Open();
Then I have another sqlite database on file which is opened as below.
SQLiteConnection src= new SQLiteConnection(#"Data Source=C:\db1.sqlite");
src.Open();
Next, I tried to VACUUM INTO SQL to copy the file database into in-memory database, and it gave me an error.
using( SQLiteCommand cmd = src.CreateCommand() )
{
cmd.CommandText = $"VACUUM main INTO 'file:db1?mode=memory&cache=shared';";
cmd.ExecuteNonQuery();
}
SQLiteException: out of memory
at System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)
at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
The database in file is small(20KB), but it says out of memory?
Thank you
Well, first of all thanks for your Vacuum into syntax example, i wasn't able to find one!
here's the solution:
SQLiteConnection dest = new SQLiteConnection("FullUri='file:db1?mode=memory&cache=shared'");
dest.Open();
SQLiteConnection src = new SQLiteConnection(#"Data Source=MYDb.sqlite");
src.Open();
using (SQLiteCommand cmd = src.CreateCommand())
{
cmd.CommandText = #"VACUUM INTO 'file:db1?mode=memory&cache=shared';";
cmd.ExecuteNonQuery();
}
using (SQLiteCommand cmd = dest.CreateCommand())
{
cmd.CommandText = "SELECT count(*) FROM sqlite_master WHERE type = 'table'";
var ret = cmd.ExecuteScalar();
}
in my case ret was equal to 9 (number of tables in 'MYDb.sqlite').
I think the problem was in the vacuum into syntax: you should not specify the source db (VACUUM INTO not VACUUM main INTO).
If you use "Microsoft.Data.Sqlite" (lightweight ADO.NET provider for SQLite) here is an example of C# code that uses VACUUM:
using (SqliteConnection connection = new SqliteConnection("Data Source=hello.db"))
connection.Open();
SqliteCommand sqliteCommand = new SqliteCommand("VACUUM", connection);
sqliteCommand.ExecuteNonQuery();
}

connecting oracle 10g enterprise with asp.net code gives error for tns 1

I'm using this code to access the oracle db of remote server, but am unable to connect
OleDbConnection oCon = new OleDbConnection();
oCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=FAIMS_DEV;Password=FAIMS_DEV;Data source=FAIMSDEV;";
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM TAB", oCon);
//OleDbDataAdapter da = new OleDbDataAdapter("SELECT COMP_CODE, CATEGORY_GRP_CODE, CATEGORY_CODE, BRIEF_DESC_ENG, SYSDATE, 'MIS_USER' FROM ICSPCT", oCon);
DataTable dt = new DataTable();
da.Fill(dt);
Your connection string looks like it is for MS Access. If you are trying to connect to Oracle, you may need to get the Oracle library.
For a connection to Oracle example, look at this question: Connect to Oracle without tnsname.ora.

How to connect to SQL Server with asp.net

I want to connect to SQL Server with ASP.NET. Can any one give me some sample program to do so?
Here's an example
http://www.beansoftware.com/ASP.NET-Tutorials/ASP.NET-SQL-Server.aspx
Good Luck!
June R's example is inserting data.
using System.Data;
using System.Data.SqlClient;
string Connection = "server=ALDAN; uid=sa; pwd=sa; database=GAZCAD; Connect Timeout=10000";
SqlConnection DataConnection = new SqlConnection(Connection);
// the string with T-SQL statement, pay attention: no semicolon at the end of //the statement
string Command = "Select * from myTable";
// create the SQLCommand instance
SQLCommand DataCommand = new SqlCommand(Command, DataConnection);
// open the connection with our database
DataCommand.Connection.Open();
// Data adapter
SqlDataAdapter da = new SqlDataAdapter(DataCommand);
// To fill data, i need a datatable.
DataTable dt = new DataTable();
// Filling my table
da.Fill(dt);

error in mysql connection "The given key was not present in the dictionary"

I have problem while connecting to mysql database using "ADO.NET Driver for MySQL (Connector/NET)"
I got this exception The given key was not present in the dictionary.
Edit:
MySqlConnection con = new MySqlConnection("Server=localhost;Database=pgs_db;Uid=root;Pwd=;");
MySqlCommand com = new MySqlCommand();
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.CommandText = "getStudent";
con.Open();
MySqlDataReader dr =com.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
sorry i was using a worng connectionstring
this is a right one :
"server=localhost; user id=user; password=password; database=mydatabase; pooling=false;"
thnx Oded
Without seeing the code, I am guessing you are trying to access a configuration key that does not exist in your application config file.
Edit:
After seeing the code sample, the problem is probably not with connecting to the DB.
I would say that in your GridView, you are binding to a field that is not returned by the DB query.

Resources