C# programming with database Basic guideline. First step Install SQL Server 2008/2010 (Professional...
Preview:
Citation preview
- Slide 1
- C# programming with database Basic guideline
- Slide 2
- First step Install SQL Server 2008/2010 (Professional edition
if possible) Install Visual Studio 2008/2010 you can begin to
do!!!
- Slide 3
- http://www.sqlserverclub.com/essentialguides/how-to-install-
sql-server-2008-step-by-step-guide.aspx
- Slide 4
- SQL Server After installation,
- Slide 5
- Slide 6
- Slide 7
- ADO.NET 7 Includes number of classes that can be used to
retrieve, manipulate, and update data in databases Can work with
databases in a disconnect manner Database table(s) can be retrieved
to a temporary file To retrieve data first, you must connect to the
database ADO.NET uses a feature called data providers to connect,
execute commands, and retrieve results from a database
- Slide 8
- Data Providers 8 Microsoft SQL Server Applications using SQL
Server 7.0 or later Oracle Applications using Oracle data sources
Object Linking and Embedding Database (OLE DB) Applications that
use Microsoft Access databases Open Database Connectivity (ODBC)
Applications supported by earlier versions of Visual Studio
- Slide 9
- Data Providers ( continued ) 9 Classes are encapsulated into a
different namespace by provider Four core classes make up each data
provider namespace Connection Command DataReader or DataSet
DataAdapter
- Slide 10
- 10 Data Providers ( continued )
- Slide 11
- C# programming language Go to Settings.settings
- Slide 12
- C# programming language Create a new class to work with
Database, for example DatabaseHelper class In this class, create
the connection such as: // declare the class level connection
variable SqlConnection conn; // init with the above connection
string in the constructure of the class conn = new
SqlConnection(global::DoAnTinHocProject.Properties.Settings.Default.DoAnConnectionString);
conn.Open();
- Slide 13
- C# programming language public DataSet SelectData(string
SQLCommand) { // hm m u th c thi cc cu l nh select, tr v d li u c
ch a trong dataset DataSet ds = new DataSet(); try { if
(sqlconn.State != ConnectionState.Open) sqlconn.Open(); SqlCommand
sqlComm = new SqlCommand(); SqlDataAdapter sqlAdp = new
SqlDataAdapter(); sqlComm.Connection = sqlconn; sqlComm.CommandText
= SQLCommand; sqlComm.CommandType = CommandType.Text;
sqlAdp.SelectCommand = sqlComm; sqlComm.ExecuteNonQuery();
sqlAdp.Fill(ds); return ds; } catch (Exception) { return null; }
}
- Slide 14
- C# programming language Public int ExecuteCommand(string
SQLCommand) { // hm m u th c thi cc cu l nh insert, update, delete)
try { if (sqlconn.State != ConnectionState.Open) sqlconn.Open();
SqlCommand sqlComm = new SqlCommand(); sqlComm.Connection =
sqlconn; sqlComm.CommandText = SQLCommand; sqlComm.CommandType =
CommandType.Text; return sqlComm.ExecuteNonQuery(); } catch
(Exception) { return -1; }
- Slide 15
- C# programming language The example of using the above
functions - To display data, use the DataGridView Control
- Slide 16
- C# programming language //Load data on datagridview control,
transfer the SELECT command into the function SelectData
DatabaseHelper h = new DatabaseHelper(); dataGridView1.DataSource =
h.SelectData("Select * from Workdays ").Tables[0];
- Slide 17
- C# programming language // To insert data, transfer the Insert
statement to the function ExecuteCommand DatabaseHelper h = new
DatabaseHelper(); h.ExecuteCommand(Insert into Workdays (ID, Day)
values (1, Monday)); // Update, Delete is similar with this
- Slide 18