Im still in the learning stages and have spent the majority of today searching around trying to follow tutorials or get information on how to simply use sql (i know the sql 'language' as have worked with databases a long time via php,vba and access but c# is new to me and not as simple to conenct to (althoguh will be once i understand how to!)
The Error i get is: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll
Additional information: System error.
my code is
{
private void button1_Click(object sender, System.EventArgs e)string istring = @"INSERT INTO tblUsers(username, distance) VALUES('Test',54321);";SqlConnection conn =
conn.Open();
new SqlConnection(@"Data Source=L:\testing\in the distance\ITD.mdb");
SqlCommand cmd =
new SqlCommand(istring, conn);// this is where it debugscmd.ExecuteNonQuery();
if (conn != null)
{
conn.Close();
}
}
Thanx
Loading
VulpesPosted Jul 20, 2011, 11:37 AM
SqlConnection is for SQL Server. For Access you need to use OleDbConnection and change the rest of the code to use this provider.
So, try:
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=L:\testing\in the distance\ITD.mdb;User Id=admin;Password=;");
and change SqlCommand to OleDbCommand.
Sam HobbsPosted Jul 21, 2011, 1:15 PM
It is possible to create a data source in VS that will generate code that makes it easier to manipulate data in a table. It does require some learning but when you know how to do it the better way, you won't need to waste time with details. There are basically two ways to create a data source; either TableAdapters or Entities.
Have a look at my Database Table Update in a DataGridView without Writing Code; it describes creation of a TableAdapter data source. It does not describe how to use TableAdapters for manipulating data but TableAdapters can be useful for that.
There are other articles in this web site about Entities. If the articles are not enough, then it will help to get a book about database programming.
It is useful for you to learn about SqlConnection and the other ADO classes. Just understand that you don't need to be concerned about detailed classes such as that if you create a data source and manipulate the data with it.
BoxPosted Jul 21, 2011, 7:50 AM
This is the code now (at the bottom)
Sam, If there are better ways of doing things I am very interested though. I am just learning at the moment so want to avaoid bad habbits early on ideally.
Basicly im just trying to manupulate data in a table at the moment.
private void button1_Click(object sender, System.EventArgs e)
{
string istring = @"INSERT INTO tblUsers(username, distance) VALUES('Test',121);";
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=L:\BDSU\Archive\Personal - TO BE SORTED\Scott Thurlow\InTheDistance.mdb; User Id=admin; Password=;");
conn.Open();
OleDbCommand cmd = new OleDbCommand(istring, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
Abhimanyu K VatsaPosted Jul 21, 2011, 4:02 AM
Sam HobbsPosted Jul 20, 2011, 11:55 PM
Jiteendra SampathiraoPosted Jul 20, 2011, 1:01 PM
new SqlConnection(@"Data Source=L:\testing\in the distance\ITD.mdb");
place it under app_data and right click on mdb file and copy the file address...
use that as connectionstring....
sqlconnection sqlconn=new sqlconnection(fileaddress).........
use this directive System.Data.Sqlclient....or System.Data.Oledb;
but check if mdb file has any access rights........
VulpesPosted Jul 20, 2011, 11:56 AM
In your 'using' directives, you'll need to change this line:
using System.Data.SqlClient;
to this:
using System.Data.OleDb;
so the compiler will be able to find the correct classes.
BoxPosted Jul 20, 2011, 11:26 AM
different error now :(
An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll
Additional information: Keyword not supported: 'provider'.
VulpesPosted Jul 20, 2011, 11:22 AM
If you're using MS Access, I'd try this instead:
SqlConnection conn = new SqlConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=L:\testing\in the distance\ITD.mdb; User Id=admin; Password=;");
BoxPosted Jul 20, 2011, 11:09 AM
Zoran HorvatPosted Jul 20, 2011, 11:04 AM
Try the same statement but only limiting the value to something smaller, e.g. this:
string istring = @"INSERT INTO tblUsers(username, distance) VALUES('Test',100);";
Zoran