9
Database Programming Dr. John Abraham

Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show

Embed Size (px)

Citation preview

Page 1: Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show

Database Programming

Dr. John Abraham

Page 2: Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show

Data Sources

• Data source specifies the source of the data for an application.

• Click on Data from Menu• Choose show data sources• Add a data source• Point to the directory where it is located• Choose the table you want• Drag the data source to the form to bind it to the

program.

Page 3: Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show

Data source Configuration Wizard

– Choose a data source type– Click on database– Choose a database model– Choose dataset– Choose the database object starting with the

computer name– Creates a connections string like this:– "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|

DataDirectory|\transcript.mdb“– Test the connection before continuing.

Page 4: Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show

Add connection

• Make sure to choose the type of database you are using such as Microsoft Access, SQL, Oracle, ODBC, etc.

• Express edition does not give all the options.

Page 5: Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show

Choose database objects for the data source

• Choose the table you want

• Choose the fields you want by checking

• Give the dataset a name

Page 6: Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show

Using a data source

• If you drag and drop a table from the Data Sources Visual studio adds a DataGrid View control.

• If you already have a dragridView control on the from, just drag the table onto it.

Page 7: Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show

Sample code.• This worked on my laptop with SQL-express installed.

private void btnSave_Click(object sender, EventArgs e){SqlConnection cs = new SqlConnection("Data Source=ABRAHAM-

LAPTOP:SQLEXPRESS1; Initial Catalog=csharp; Integrated Security=True");

cs.Open();//MessageBox.Show(cs.State.ToString());SqlDataAdapter dc = new SqlDataAdapter();dc.InsertCommand = new SqlCommand("INSERT INTO CONTACTS VALUES

(@Fname, @Lname,@Tele)",cs);dc.InsertCommand.Parameters.Add("@Fname",

SqlDbType.VarChar).Value=textFname.Text;cs.Close();}

Page 8: Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show

Code for LINQ to SQLThis also worked on my notebook computer with SQL express

installed.• static void Main(string[] args){DataClasses1DataContext mine = new DataClasses1DataContext();var st = from CONTACT in mine.CONTACTs select CONTACT;foreach (var s in st)System.Console.WriteLine(s.Fname + s.Lname + s.Tele);System.Console.ReadKey();}Here is the output• John                Abraham          3550

Pearl               Brazier             3455

Page 9: Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show