The current problem I'm having is an error message that it could not find file C: now I dont know what the problem is because the file is in that location. I have I tried in both .accbd and .mbd.
private static OleDbConnection GetConnection()
{
OleDbConnection conn = new OleDbConnection();
String connectionString =
#"Provider=Microsoft.JET.OlEDB.4.0;"
+ #"Data Source= C:\Temp\F1\Docs\Expeditors Project\Table1.accbd";
conn = new OleDbConnection(connectionString);
conn.Open();
return conn;
}
Do you tried another Provider?
For example:
Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Temp\F1\Docs\Expeditors Project\Table1.accbd;
try \\ in Data Source Path
like below -
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Data\test.mdb;Persist Security Info=False");
try this `
`private static OleDbConnection GetConnection() throws SQLException{
{
if (conn==null)
{
try{ OleDbConnection conn = new OleDbConnection();
String connectionString = #"Provider=Microsoft.JET.OlEDB.4.0;"
+ #"Data Source= C:\Temp\F1\Docs\Expeditors Project\Table1.accbd";
conn = new OleDbConnection(connectionString);
conn.Open();
return conn;
}}
catch(Exception e){
e.printStackTrace();
}
Related
When I am trying to open a excel into one of my windows service using following code it is throwing "Value cannot be null. Parameter name: source" on objConn.Open(); Can any one please help me.
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
LogManager LogWrite = new LogManager();
try
{
string conn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Repository\RuleExcel\Rules_Repository_2018-06-28_03-41-29-133.xlsx;Extended Properties='Excel 12.0;HDR=YES;';";
LogWrite.WriteLog(conn);
// Create connection object by using the preceding connection string.
objConn = new OleDbConnection(conn);
LogWrite.WriteLog(objConn.DataSource);
// Open connection with the database.
objConn.Open();
try this below code, it works for me :
using (OleDbConnection objConn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + #";Extended Properties=""Excel 12.0;IMEX=1;HDR=YES;"""))
{
objConn.Open();
}
public static DataSet ParseDatabaseData(string sheetName)
{
string connectionString = "Provider=System.Data.SqlClient;Data Source= MHSPC56888_VM1\\SQLEXPRESS;Initial Catalog=xxxxxxx;User id=xx;Password=xxxxx;";
SqlConnection conn = new SqlConnection(connectionString);
string strSQL = "SELECT * FROM [" + sheetName + "$]";
SqlCommand cmd = new SqlCommand(strSQL, conn);
conn.Open();
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dataset);
conn.Close();
return dataset;
}
The error show that 'provider' keyword is wrong.
Please help me to correct how to connect with Database through connection string?
You do not need to specify the Provider in the Connection String.
Try it like this:
public static DataSet ParseDatabaseData(string sheetName)
{
string connectionString = "Data Source= MHSPC56888_VM1\\SQLEXPRESS;Initial Catalog=xxxxxxx;User id=xx;Password=xxxxx;";
Instead of mentioning the connection string in the individual file itself, you can place the connection string in the web.config or app.config and use the config where ever required.
Sample for web.config place the connection string under the <configuration>, there you can provide the provider name:
<configuration>
<connectionStrings>
<add name="ConnString"
connectionString="Data Source= MHSPC56888_VM1\\SQLEXPRESS;Initial Catalog=xxxxxxx;User id=xx;Password=xxxxx;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
and inside the file
public static DataSet ParseDatabaseData(string sheetName)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
Note: add using System.Configuration; for the ConfigurationManager.
I have to connect my code to the access database but mainly, have to provide clear exception if that database file is not located in given location (like file not found). For this code :
string connStr =( #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\test.accdb;Persist Security Info=False");
OleDbConnection conn1 = new OleDbConnection();
conn1.ConnectionString = connStr;
OleDbCommand cmd = conn1.CreateCommand();
cmd.CommandText = "INSERT INTO customer (id, name)" + " VALUES('3', 'C');";
conn1.Open();
cmd.ExecuteNonQuery();
I want to display message if test database is not present there. What can I do ? please suggest. thank you
I think you can use the static method File.Exists:
if(!File.Exists("Z:\\test.accdb"))
throw new FileNotFoundException();
try
{
string connStr =( #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\test.accdb;Persist Security Info=False");
OleDbConnection conn1 = new OleDbConnection();
conn1.ConnectionString = connStr;
OleDbCommand cmd = conn1.CreateCommand();
cmd.CommandText = "INSERT INTO customer (id, name)" + " VALUES('3', 'C');";
conn1.Open();
cmd.ExecuteNonQuery();
}
catch(Exception e)
{
//print the message you want;
}
I´m trying to use a database inside my solution as local database, but i Don´t know how I can reference it in my connection string.
Usuually i use external database and I reference it like this
SqlConnection miCon = new SqlConnection(#"Data S...")
miCon.Open();
SqlDataAdapter miDa = new SqlDataAdapter("select distinct tipo from ejercicios", miCon);
DataSet miDs = new DataSet();
miDa.Fill(miDs);
foreach (DataRow row in miDs.Tables[0].Rows)
{
comboBox1.Items.Add(row[0].ToString());
}
miCon.Close();
Well how would you know.... Google first result!
SQL Express:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;
var con = new SqlConnection(#"Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;");
LocalDB:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\mydbfile.mdf;Integrated Security=True
var con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\mydbfile.mdf;Integrated Security=True");
http://www.connectionstrings.com/sql-server-2008
http://www.connectionstrings.com/sql-server-2012
create a class with the following code
private static readonly SqlConnection SqlConnection = new SqlConnection("Data Source=server name;Initial Catalog=database name;Persist Security Info=True;User ID= ;Password= ");`
public SqlConnection ServerConnection()
{
if (SqlConnection.State == ConnectionState.Open)
{
SqlConnection.Close();
}
else
{
SqlConnection.Open();
}
return SqlConnection;
}
and the call accordingly in the forms you want
How can I replace D:\temp\test.xls with filePath in OleDbConnection statement.
I can get the exactly filePath (with OpenFileDialog, then I can located my .xls file conveniently, no more hardcoded), but when I insert the variable filePath as Style2, it doesn't work. How can I fix this ? Thanks.
Style1
OleDbConnection dbConnection = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;Extended Properties=""Excel 8.0;HDR=Yes;""");
Style2
OleDbConnection dbConnection = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=filePath;Extended Properties=""Excel 8.0;HDR=Yes;""");
[Updated]
Some part of my code as this,
DataTable fooData = new DataTable();
OleDbConnection dbConnection = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=filePath;Extended Properties=""Excel 8.0;HDR=Yes;""");
dbConnection.Open ();
try
{
OleDbCommand dbCommand = new OleDbCommand("SELECT * FROM [maleSheet$]", dbConnection);
OleDbDataReader dbReader = dbCommand.ExecuteReader();
int RankIndex = dbReader.GetOrdinal("Rank");
while (dbReader.Read())
{
string rank = dbReader.GetValue(RankIndex).ToString();
////....
}
}
Error as below at line OleDbDataReader dbReader = dbCommand.ExecuteReader();
An unhandled exception of type
'System.Data.OleDb.OleDbException'
occurred in System.Data.dll
OleDbConnection dbConnection = new OleDbConnection( String.Format( #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=Yes;""", filePath ) );