What is the best way to use a database in c#?
using System.Data.OleDb;
...
using (OleDbConnection conn = new OleDbConnection())
{
conn.ConnectionString = "Provider=sqloledb;Data Source=yourServername\yourInstance;Initial Catalog=databaseName;Integrated Security=SSPI;";
using (OleDbCommand cmd = new OleDbCommand())
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "Select * from yourTable";
using (OleDbDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
Console.WriteLine(dr["columnName"]);
}
}
}
}
Tags: c# database connection loops
Source: By Michael Pryor as answer to the question
This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 2.5
Related code-snippets:
- How do you convert decimal in C#?
- How do you calculate someone's age based on datetime?
- How can I calculate relative time using a C# language?
- If a query result set is filled in LINQ with data then fill the data set or the table with data. This example is just that.
- Binary data in MySQL.
- How do I get a distinct, ordered list of names from a data table using LINQ?
- Flat file databases are generally stored in a computer hard drive. Flat file databases cannot be shared by any party with another.
- Decoding T-SQL CAST in C#/VB.NET.
- Do you like compression of files? How do you decompress them?
- How do I upgrade my SQL Server database version?
- How can I print an HTML document from a web service?
- What is the catch all algorithm?
- How do you sort a dictionary by value?
- WinForms ComboBox data binding gotcha WinForms combobox data binding gotcha WinForms ComboBox data binding gotcha WinForms ComboBox data binding gotcha WinForms ComboBox data binding gotcha WinForms ComboBox data binding gotcha WinForms ComboBox data binding gotcha gotcha
- What is the difference between Int and Integer in Java and C#?