Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Database Programming Dr. John Abraham

2 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.

3 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.

4 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.

5 Choose database objects for the data source Choose the table you want Choose the fields you want by checking Give the dataset a name

6 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.

7 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(); }

8 Code for LINQ to SQL This 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

9


Download ppt "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."

Similar presentations


Ads by Google