

Sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1003, 'Rakesh M', 'Nag Chowk, Jabalpur M.P.', 43.43) " Sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1002, 'Anoop Singh', 'Lodi Road, DELHI', 353.64) " Sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1001, 'Puneet Nehra', 'A 449 Sect 19, DELHI', 23.98 ) " Sql = "CREATE TABLE myTable" + "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," + "myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)" Private Sub CreateTableBtn_Click( ByVal sender As Object, ByVal e As System.EventArgs) Listing 3 creates a table myTable with four column listed in Table 1. In this statement you define the table and schema (table columns and their data types).

You use CREATE TABLE SQL statement to create a table. Private Sub CreateDBBtn_Click( ByVal sender As Object, ByVal e As System.EventArgs)Ĭonn = New SqlConnection(ConnectionString)ĭim sql As String = "CREATE DATABASE mydb ON PRIMARY" + "(Name=test_data, filename = 'C:\mysql\mydb_data.mdf', size=3," + "maxsize=5, filegrowth=10%)log on" + "(name=mydbb_log, filename='C:\mysql\mydb_log.ldf',size=3," + "maxsize=20,filegrowth=1)" 'This method creates a new SQL Server database Listing 2 creates a SQL Server database mydb and data files are stored in the C:\\mysql directory. Depending on the database type, you can also set values of database size, growth and file name. The syntax of CREATE DATABASE depends on the database you create. The CREATE DATABASE SQL statement creates a database. If conn.State = ConnectionState.Open ThenĬonnectionString = "Integrated Security=SSPI " + "Initial Catalog=mydb " + "Data Source=localhost "Īfter this I'm going to create a new SQL Server database. Private Sub ExecuteSQLStmt( ByVal sql As String) Our application form looks like Figure 1.
#How to connect database in vb net 2010 code
You can even test code by adding only one button or one button for each activity. To test this application, create a Widows application add a data grid control and some button controls. In this application, I'll create a SQL Server database, create a database table, add data to it, create database objects such as views, stored procedures, rules, and index and view data in the data grid using Sql data provider.
#How to connect database in vb net 2010 how to
This example shows you how to create a new database table, add data to it, create a view of the data, alter database table and then delete the newly created table. It also provides commands to alter a database and database schemas for example adding and deleting a column from a database table, adding some constraints to a column and so on. Using SQL statements you can create database objects programmatically such as a table, view, stored procedure, rule, index and so on. MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.SQL not only let you select, add, and delete data from databases table, it also provides commands to manage databases. MsgBox("Student with Nis " & Trim(cmbNis.Text) & " failure to added", MsgBoxStyle.OKOnly, "Message :") MsgBox("Student with Nis " & Trim(cmbNis.Text) & " succesfully added", MsgBoxStyle.OKOnly, "Message :") MsgBox("medical dengan Nis " & Trim(cmbNis.Text) & " Sudah ada didata base", MsgBoxStyle.OKOnly, "Message :")Ĭmdmedical1.CommandText = "INSERT INTO medical(Idmedical, Nis, BloodType, Disease,AnomalyBody,Height,Weight) VALUES('" & Trim(idmedical.Text) & "','" & Trim(cmbNis.SelectedItem) & "','" & Trim(cmbBloodType.SelectedItem) & "','" & Trim(txtdisease.Text) & "','" & Trim(txtAnomalyBody.Text) & "','" & Trim(txtHeight.Text) & "','" & Trim(txtWeight.Text) & "')"Ĭheck = () If MsgBox("Are you sure to store student medical data with Nis : " & cmbNis.SelectedItem & " ?", MsgBoxStyle.OKCancel, "Input confirm") = MsgBoxResult.Cancel ThenĬmdmedical.CommandText = "SELECT * FROM medical WHERE Nis='" & Trim(cmbNis.SelectedItem) & " ' " MsgBox("Student Medical Data is not completed", MsgBoxStyle.OKOnly) If cmbNis.SelectedIndex = -1 Or cmbBloodType.SelectedIndex = -1 Or txtdisease.Text = "" Or txtAnomalyBody.Text = "" Or txtHeight.Text = "" Or txtBerat.Text = "" Then This simple following code to add/store data to sqlserever : Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
