OleDbConnection connect = new OleDbConnection ("Provider=Microsoft.ACE.OLEDB.12.0;Data source:C:\\Users\\PC\\Documents\\Visual Studio 2013\\Projects\\WindowsFormsApplication1\\WindowsFormsApplication1\\Firebird damagem0.accdb;Persist Security Info=False");
public partial class Form3 : Form
{
// OleDbconnection database
OleDbConnection connect = new OleDbConnection ("Provider=Microsoft.ACE.OLEDB.12.0;Data source:C:\\Users\\PC\\Documents\\Visual Studio 2013\\Projects\\WindowsFormsApplication1\\WindowsFormsApplication1\\Firebird damagem0.accdb;Persist Security Info=False");
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Set up command
connect.Open();
OleDbConnection command = new OleDbConnection("SELECT [Damage columns], ID FROM [Copy of Firebird m0 damage]; connection");
command.ExecuteNonQuery();
DataTable ds = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter (command);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox3.Items.Add(dr["[Damage columns]"].ToString());
}
connect.Close();
}
Error occurs at connect.Open(); I've done everything right but i still keep receiving the error, any suggestions?
This error is typically due to an invalid connection string.
Since you have a space in your datasource path, try wrapping it with single quotes. Also, it should be
Data Source=
not
Data Source:
You connection string looks to be wrong with a semicolon after Data Source.
OleDbConnection connect = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data source=C:\Users\PC\Documents\Visual Studio 2013\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Firebird damagem0.accdb;Persist Security Info=False");
Also you have another OleDbConnection there in code which should be OleDbCommand.
And finally, did you install the access database engine?
I just managed to produce a very similar error out of thin air (it worked before) and after a while of digging around what I changed since then I finally found the mistake:
Provider=Microsoft.Jet.OleDb.4.0;Data Source=myfile.mdb
works, while
Provider=Microsoft.Jet.OleDb.4.0;DataSource=myfile.mdb
produces
Installable ISAM not found
For those who don't see it: The difference is the spelling of the key Data Source vs DataSource - the space character is important here!
Related
I designed a C# desktop app in visual studio 2019 and for database used sql server express 2019 edition. i am trying to run this app on another pc. i have installed sql server express 2019 in the other pc also MS server management studio 2019 installed and restored the database. everything works fine like login,saving updating,deleting but when i try to fetch data to datagridview it shows "system.data.sqlclient.sqlexception - a network related or instance specific error occurred while establishing a connection to sql server. the server was not found or was not accessible. verify instance name is correct and sql server is configured to allow remote connections.(provider: sql network interfaces, error: 26 - error locating server/instance specified)."
all the ports are enabled and firewall rule is also enabled in the client pc.
i am using the below connection string for the connection.
class Connection
{
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;Initial Catalog=icon;Integrated Security=True");
public SqlConnection active()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
return con;
}
}
Please help anyone as i am not able to get what is the problem going on.
Belowcode is working
private void loginBtn_Click(object sender, EventArgs e)
{
Connection con = new Connection();
SqlCommand cmd = new SqlCommand("select * from [user] where
Username='" + usernameTxt.Text + "'and password='" + passwordTxt.Text
+ "'", con.active());
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("Login Successful", "Sucsess",
MessageBoxButtons.OK, MessageBoxIcon.Information);
new dashboard().Show();
this.Hide();
}
but this is not working.it shows the error when i try to fetch the data.
public partial class AllSudent : Form
{
public AllSudent()
{
InitializeComponent();
}
Connection con = new Connection();
public int studentID;
private void AllSudent_Load(object sender, EventArgs e)
{
GetStudentsRecord();
}
public void GetStudentsRecord()
{
SqlCommand cmd = new SqlCommand("Select * From [student]",
con.active());
DataTable dt = new DataTable();
SqlDataReader sdr = cmd.ExecuteReader();
dt.Load(sdr);
sdataGridView.DataSource = dt;
}
Throw your Connection class away, and pass the connection string to the DataAdapter. Don't bother opening or closing the connection; DataAdapter knows how to open a connection if it's closed
Put the connectionstring into the Settings
Use parameters
private void loginBtn_Click(object sender, EventArgs e)
{
using(var sda = new SqlDataAdapter("select * from [user] where Username=#user and password=#pass", Properties.Settings.Default.ConStr)
{
//USE PARAMETERS
sda.SelectCommand.Parameters.Add("#user", SqlDbType.VarChar, usernameTxt.Text.Length).Value = usernameTxt.Text;
sda.SelectCommand.Parameters.Add("#pass", SqlDbType.VarChar, passwordTxt.Text.Length).Value = passwordTxt.Text.GetHashcode(); //DO NOT store your passwords in plain text!!
var dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("Login Successful", "Sucsess",
MessageBoxButtons.OK, MessageBoxIcon.Information);
new dashboard().Show();
this.Hide();
}
}
}
Use parameters
Just in case you missed it: USE PARAMETERS. Never again, in your life ever, should you concatenate a value into an SQL string. Ever. There is no reason to do it, and doing it will result in the software you create being hacked / you getting fired / both
Also, don't store passwords in plain text, ever. Salt and hash them. I've used string.GetHashcode() for demo purposes, which is not good but better than plaintext
Do the same thing to the not working code:
public void GetStudentsRecord()
{
using(var sda = new SqlDataAdapter("Select * From [student]", Properties.Settings.Default.ConStr)){
var dt = new DataTable();
sda.Fill(dt);
sdataGridView.DataSource = dt;
}
}
this issue also confusing me a few days after the IT guy do some security settings to the SQL Server. i have an EntityFramework for the Web application and a desktop application. after i did some setting on the SQL Server, the Web application comeback to work, but the desktop still with issue. but i used the some connection string for the both application, it make no sense one is work but the other doesn't. then i searched a lot until i found some one said need add a port number 1433 after the $ServerName$DatabaseInstanceName,1433 at here http://www.windows-tech.info/15/9f6dedc097727100.php . after i added it. the exception became: System.Data.SqlClient.SqlException: Login failed for user 'domain\name-PC$'. then i found this link System.Data.SqlClient.SqlException: Login failed for user: System.Data.SqlClient.SqlException: Login failed for user it said need add Trusted_Connection=False;. the whole connection string should be like: data source=XXXXX\SQLSERVER,1433;initial catalog=XXXDB;user id=UserID;password=PWD;Trusted_Connection=False;MultipleActiveResultSets=True;
hope this answer will help the ones out off Generic exception: "Error: 26-Error Locating Server/Instance Specified)
I'm trying to pass data from DataGridView to a Database SqlClient I opened especially for this. When the program runs, it tells me this error:
An attempt to attach an auto-named database for file failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
I made a form with 2 buttons, one for adding data to the DataGridView (works fine) and another one to pass the data to a database table.
the not working button's code is this:
private void button3_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=.;Integrated Security=True;Connect Timeout=30;User Instance=True";
string sql = "SELECT * FROM Authors";
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
con.Open();
dataadapter.Fill(ds, "Authors_table");
con.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Authors_table";
}
What can I do to fix it?
I tried choosing a data source to the DataGridView but it still didn't work out.
Thanks in advance!
I'm trying to access a .xlsx file which I want to read and I'm doing it like that :
protected void btnImportList_Click(object sender, EventArgs e) {
string connnection = #"Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\Users\Karl\Desktop\NESR data\Autoload Sample - Goals.xlsx;Extended Properties=\Excel 12.0 xml;HDR=YES;IMEX=1\;";
OleDbConnection con = new OleDbConnection(connnection);
OleDbCommand command = new OleDbCommand();
DataTable dt = new DataTable();
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [DL JV + VS Final$]", con);
myCommand.Fill(dt);
Console.Write(dt.Rows.Count);
}
However, I'm getting the following error message :
Could not find installable ISAM
I installed the Microsoft AccessDatabase Manager as it was said on the internet but I'm still trying to figure out what more I can do to fix that. Any idea?
Use the below connection string,
string connnection = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Karl\Desktop\NESR data\Autoload Sample - Goals.xlsx; Extended Properties=Excel 12.0;"
There should be a space between 'Data' and 'Source' and removed HDR=YES, IMEX=1.
I have seen lots of answers to connect to MS Access via OleDB but there is not good answer for SQL Server. I try to connect to a SQL Server database via OleDB provider in my C# program.
This is the connection string I am providing.
Provider=SQLOLEDB;Data Source=<servername>;Initial Catalog=<dbname>;Integrated Security=SSPI
But it gives me error
‘Keyword not support ‘Provider’’
What I want to do here is connect database via OleDB in C# program.
This works as expected on my side. From the error message I strongly suspect that you are using the SqlConnection class instead of the OleDbConnection (Of course you need to use all the other classes provided by OleDb like OleDbCommand, OleDbDataReader etc...)
string connStr = "Provider=SQLOLEDB;Data Source=<servername>;Initial Catalog=<dbname>;Integrated Security=SSPI";
using(OleDbConnection cnn = new OleDbConnection(connStr))
{
....
}
When in doubt, use the string builder in visual studio. That way unsupported keywords can't creep into your connection strings, the following is a wonderful example on how to use it.
http://www.c-sharpcorner.com/uploadfile/suthish_nair/how-to-generate-or-find-connection-string-from-visual-studio/
The same Connection string is working fine at my end.
I am posting my sample code which is executes successfully at my end
public string connStr = "Provider=SQLOLEDB;Data Source=.;Initial Catalog=<dbName>;Integrated Security=SSPI";
public OleDbConnection con;
protected void Page_Load(object sender, EventArgs e)
{
Test();
}
public void Test()
{
con = new OleDbConnection(connStr);
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from tblApartments", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
}
Please place breakpoint and check line to line and when your breakpoint comes to con.close(); then check ds, you can see the output.
The connection string you're using, considering the OLE DB provider, is correct. I didn't find any error in the connection string used, if you want to connect to a SQL Server data source.
Most probably, the reason of that error should be that you're not using correctly all the classes and objects required by the OLE DB provider, like OleDbCommand (that is similar to a SqlCommand but it's different), OleDbConnection, OleDbDataAdapter and so on. In a nutshell, the reason of that error should be this:
string connStr = "Provider=SQLOLEDB;Data Source=<servername>;Initial Catalog=<dbname>;Integrated Security=SSPI";
using(SqlConnection scn = new SqlConnection(connStr))
{
....
}
Indeed, using a SqlConnection object, the ConnectionString property doesn't support the keyword Provider and, executing your application, you got an error about a keyword not supported.
Have a look at this simple tutorial about the use of OLE DB provider.
I'm developing a Windows CE app in C# and trying to connect to an Oracle database. I'm using CoreLab.Oracle reference. This is my code:
using CoreLab.Oracle;
namespace SmartDeviceProject1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OracleConnection conn = new OracleConnection();
conn.ConnectionString = "User ID=name;Password=pass;Host=ip;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;Port=1522;Sid=bleh;Unicode=True";
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "Select * from dc_emp ";
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
dr.Read();
label1.Text = dr.GetString(0);
conn.Dispose();
}
}
}
Every time I run the project conn.Open(); gets error : Network error:: A Socket operation was attempted to an unreachable host. So something is wrong with my connection string but I don't know what.
I might add that when I drag and drop a "oracleConnection" component to my form in design mode and edit the properties, my connection is created.
I have read in some forums I must set the "direct" property to true in my connection string, but when I add it to my connection string it says : Unknown connection string parameter Direct
Can someone please help me?
This is now working! FINALLY FIGURED IT OUT. Had to install Virtual PC 2007 for the VS Emulators. Then configure the Emulator to use the virtual network card.