***EDIT*** Actually, this is not a problem, code works. I am just seeking the better ways using latest available tools for C#.
***END EDIT***
Following is the connection I use for an Access DB. Would you please let me know if this connection method is ok for C# or there is a better way. I am new in C#, coming from VB6 world. I would like to learn the best and easiest way to do things as much as possible... Thanks, Leo
C#
string SqlStr;
ADODB.Connection Conn1;
ADODB.Recordset Rs1;
Conn1 = new ADODB.Connection();
Rs1 = new ADODB.Recordset();
Conn1.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Leo\\Documents\\Visual Studio 2008\\Projects\\CodeDatabase\\CodeDatabase\\CodeDatabase.mdb;Persist Security Info=False;", "", "", 0 );
SqlStr = "SELECT * FROM Codes";
// Open Recordset
Rs1.Open (SqlStr, Conn1, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockReadOnly, 0);
// Check if anything in the tableif (Rs1.EOF == false)
// If so, loop and get all the information to fill the listdo
{
lstSubjects.Items.Add(Rs1.Fields["ID"].Value + "~" + Rs1.Fields["_Subject"].Value);
Rs1.MoveNext();
}
while (Rs1.EOF == false);
// Call CloseDatabase
Conn1.Close();
Conn1 = null;
}
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Sam HobbsRank #107 · 20.5K reputationPosted Feb 23, 2010, 5:12 PM EST
0
You can use the Server Explorer to connect to a database, then use the Data Sources window to create a data source. Then you can drag the data source to a blank form. By default, a DataGridView will be created in the form with a navigation bar above it. If you build the project as-is, you have a functioning database program. If the SQL for the data source is for a single table (no joins) then the program can insert, update and delete records. All of that without writing any code. What you have is good but the IDE can do a lot for you.
One of the most useful parts of a data source is TableAdapters; there is not a lot of documentation of them since they exist as generated code. A TableAdapter provides typed methods and properties that make it easier to use databases.
See Visual C++ Guided Tour for a worthwhile tour, except you don't need to do them in the order they appear.
0
Leo KoachRank #2,348 · 604 reputationPosted Mar 10, 2010, 10:20 AM EST
0
I had to change my code while doing some more enhancements on my C# study program. Now I use XML to keep three field data.
Sam HobbsRank #107 · 20.5K reputationPosted Feb 23, 2010, 5:12 PM EST
One of the most useful parts of a data source is TableAdapters; there is not a lot of documentation of them since they exist as generated code. A TableAdapter provides typed methods and properties that make it easier to use databases.
See Visual C++ Guided Tour for a worthwhile tour, except you don't need to do them in the order they appear.
Leo KoachRank #2,348 · 604 reputationPosted Mar 10, 2010, 10:20 AM EST
Here is my blog link to see all the codes...
http://www.vbdotnetheaven.com/Blogs/BlogDetail.aspx?BlogId=1951