![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 8
| c# and Microsoft access |
| -Od|n- is offline | |
| | #2 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| If you're talking about connecting to the database (like using OleDbConnection) and building a query (like "SELECT * FROM ...") then you can do it in any order you want. To actually run the query you have to be connected to the database first, of course.
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline | |
| | #3 | |
| Registered User Join Date: Jul 2009
Posts: 8
| Quote:
do u know what i can do to open up access 2007? | |
| -Od|n- is offline | |
| | #4 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| Check your connection strings, it seems Access 2000 and Access 2007 uses different strings: ConnectionStrings.com - Forgot that connection string? Get it here!
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline | |
| | #5 |
| Registered User Join Date: Jul 2009
Posts: 8
| well im getting this probrlem now i connected my database with c# and am getting "ExecuteNonQuery: connection property has not been initialized" Code: public partial class frmUpdateSupplier : Form
{
private OleDbConnection mDB;
public frmUpdateSupplier()
{
InitializeComponent();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
string supplierId;
string companyName;
string firstName;
string lastName;
string street;
string country;
string zipCode;
string phoneNo;
string mobileNo;
string emailAddress;
string sql;
OleDbCommand cmd;
supplierId = txtSupplierId.Text;
companyName = txtCompanyName.Text;
firstName = txtFirstName.Text;
lastName = txtLastName.Text;
street = txtStreet.Text;
country = txtCountry.Text;
zipCode = txtZipCode.Text;
phoneNo = txtPhoneNo.Text;
mobileNo = txtMobile.Text;
emailAddress = txtEmailAdd.Text;
try
{
sql = "UPDATE Supplier SET "
+ "Company = " + toSql(companyName) + ","
+ "FirstName = " + toSql(firstName) + ","
+ "LastName = " + toSql(lastName) + ","
+ "Street = " + toSql(street) + ","
+ "Country = " + toSql(country) + ","
+ "ZipCode = " + toSql(zipCode) + ","
+ "PhoneNo = " + toSql(phoneNo) + ","
+ "MobileNo =" + toSql(mobileNo) + ","
+ "EmailAddress =" + toSql(emailAddress)
+ "WHERE SupplierID = " + toSql(supplierId);
cmd = new OleDbCommand(sql, mDB);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private string toSql(string stringValue)
{
return "'" + stringValue.Replace("'", "''") + "'";
}
private string toSql(int intValue)
{
return intValue.ToString();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}
|
| -Od|n- is offline | |
| | #6 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| mDB seems to be null. Create a connection object (OleDbConnection) to it before using it in the command. I suggest a using-statement, so you won't have to manually clean it up: Code: using(var c = new OleDbConnection(...))
{
c.Open();
var cmd = new OleDbCommand(...);
...
smd.ExecuteNonQuery();
}
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline | |
| | #7 |
| Registered User Join Date: Jul 2009
Posts: 8
| hmph i dont really understand what you typed there sorry im kinda noob, instead of Code: private OleDbConnection mDB; Code: OleDbConnection mDB = new OleDbConnection(); Code: cmd = new OleDbCommand(sql, mDB);
cmd.ExecuteNonQuery();
Between thanx again for all the help u are giving me... |
| -Od|n- is offline | |
| | #8 |
| ...and never returned. Join Date: Aug 2009
Posts: 41
| Code: cmd = new OleDbCommand(queryString, connection) Code: cmd.ExecuteNonQuery() # returns no value Code: cmd.ExecuteScalar() #returns a single value cmd.ExecuteReader() # returns a record-set (multiple values) Code:
OleDbConnection dbCon = new OleDbConnection(connectionString);
OleDbCommand cmd = dbCon.CreateCommand();
cmd.CommandText =
@"UPDATE Students SET Address=@Address"; // the first @ symbol denotes a string literal
// the second denotes a parameter
cmd.Parameters.AddWithValue("@Address", "1 Park Ave. New York, NY. 00000");
dbCon.Open();
cmd.ExecuteNonQuery();
dbCon.Close();
Sql injection can occur when users can explicitly enter data into some field in your form (namely textboxes). Consider the following code snippet: Code: cmd.CommandText = "DELETE FROM Students WHERE Name='" + myTextBox.Text + "'"; Code: Fred OR 1=1 Some commands will return values. To return a single value, use ExecuteScalar() Code: OleDbConnection dbCon = new OleDbConnection(connectionString); OleDbCommand cmd = dbCon.CreateCommand(); cmd.CommandText = @"SELECT COUNT(*) FROM Students"; // will count the total number of students in the Students table dbCon.Open(); int numStudents = Convert.ToInt32(cmd.ExecuteScalar()); dbCon.Close(); Code:
OleDbConnection dbCon = new OleDbConnection(connectionString);
OleDbCommand cmd = dbCon.CreateCommand();
cmd.CommandText = @"SELECT StudentID,Name,Address FROM Students WHERE Class='CS101'";
dbCon.Open();
OleDbDataReader studentInfo = cmd.ExecuteReader();
while(studentInfo.Read())
{
// Display all the students information enrolled in CS101
Response.Write(studentInfo["StudentID"].ToString() + " " +
studentInfo["Name"].ToString() + " " + studentInfo["Address"].ToString() + "<br />");
}
studentInfo.Close();
dbCon.Close();
Code:
sql = "UPDATE Supplier SET "
+ "Company = " + toSql(companyName) + ","
+ "FirstName = " + toSql(firstName) + ","
+ "LastName = " + toSql(lastName) + ","
+ "Street = " + toSql(street) + ","
+ "Country = " + toSql(country) + ","
+ "ZipCode = " + toSql(zipCode) + ","
+ "PhoneNo = " + toSql(phoneNo) + ","
+ "MobileNo =" + toSql(mobileNo) + ","
+ "EmailAddress =" + toSql(emailAddress)
+ "WHERE SupplierID = " + toSql(supplierId);
Code:
@"UPDATE Supplier SET
Company=@Company,FirstName=@FirstName,LastName=@LastName,Street=@Street,
Country=@Country,ZipCode=@ZipCode,PhoneNo=@PhoneNo,MobileNo=@MobileNo,
EmailAddress=@EmailAddress
WHERE SupplierID=@SupplierID";
// then add the parameters:
cmd.Parameters.AddWithValue("@Company", toSql(CompanyName));
cmd.Parameters.AddWithValue("@FirstName", toSql(firstName));
cmd.Parameters.AddWithValue("@LastName", toSql(lastName));
cmd.Parameters.AddWithValue("@Street", toSql(street));
cmd.Parameters.AddWithValue("@Country", toSql(country));
cmd.Parameters.AddWithValue("@ZipCode", toSql(zipCode));
cmd.Parameters.AddWithValue("@PhoneNo", toSql(phoneNo));
cmd.Parameters.AddWithValue("@MobileNo", toSql(mobileNo));
cmd.Parameters.AddWithValue("@EmailAddress", toSql(emailAddress));
cmd.Parameters.AddWithValue("@SupplierID", toSql(supplierID));
concatenating the query string all those times! EDIT: In my examples above, I used the SQL way of parameterizing queries, apparently ms access does things differently (big surprise). Instead of: Code: @"SELECT Name FROM Students WHERE Class=@Class"; // Just for example Code: @"SELECT Name FROM Students WHERE Class=?"; Code:
cmd.Parameters.Add("Class", someValueForClass);
urgh. ms access sux. Last edited by StainedBlue; 09-09-2009 at 09:38 PM. |
| StainedBlue is offline | |
| | #9 |
| Registered User Join Date: Jul 2009
Posts: 8
| Hey guys thanx a lot its much clearer now. my friends said that mysql was harder than ms access..anyway after this im going to try it on mysql. Last edited by -Od|n-; 09-10-2009 at 05:34 PM. |
| -Od|n- is offline | |
| | #10 |
| ...and never returned. Join Date: Aug 2009
Posts: 41
| >>my friends said that mysql was harder than ms access..anyway after this im going to try it on mysql. Well, you're using "SQL" anyway, you're just using MS Access' flavor of SQL. SQL is not a physical thing, it's a programming language (more accurately, a "querying language"). So the structure of your program will not change, in fact, your program is simple enough, you can change anywhere it says "OleDb" to "Sql". Like: Code: using System.Data.OleDb; // To: using System.Data.SqlClient; // And: OleDbConnection oledbcon = new OleDbConnection(oledbconString); // To: SqlConnection sqlcon = new SqlConnection(sqlconString); // And: OleDbCommand oledbcmd // ... // To: SqlCommand sqlcmd // ... // And the query strings will remain the same. But I actually think Access is harder, because it's implementation of SQL is so broke. There are many syntactical differences, and many functions are just non-existent. SQL is an ANSI standard, and all the other Sql databases (MySql, Oracle, MS Sql Server) implement that standard very well. This means no matter which DB platform you're using, you can write the exact same SQL statements and 99% of the time you will produce the same exact output. MS Access is no where near this. So when you run into trouble, don't expect much help with Access, the documentation and knowledge base is simply not there. With that said, there's nothing wrong with Access, but Access is for much simpler business logic (few tables, few records, little interaction). but if you continue to work with DB's, you will have a much easier time dealing with a standards-compliant one. I suggest MS SQL Server Express, it's free, it's extremely powerful, and comes with a very nice GUI (Management Studio). Last edited by StainedBlue; 09-11-2009 at 08:24 AM. |
| StainedBlue is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|