Imports System.Data.OleDb
Public Class _default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myConn As OleDbConnection
Dim cmd As OleDbCommand
Dim sqlString, SId, FName, LName, Rank As String
SId = TextBox1.Text
FName = TextBox2.Text
LName = TextBox3.Text
Rank = TextBox4.Text
myConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Database1.mdf;")
myConn.Open()
sqlString = "INSERT INTO Students (StudentId, FirstName, LastName, Rank) VALUES ('" + SId + "','" + FName + "', '" + LName + "', '" + Rank + "')"
cmd = New OleDbCommand(sqlString, myConn)
cmd.ExecuteNonQuery()
myConn.Close()
End Sub
End Class
Imports System.Data.OleDb
Public Class _default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myConn As OleDbConnection
Dim cmd As OleDbCommand
Dim sqlString, SId, FName, LName, Rank As String
SId = TextBox1.Text
FName = TextBox2.Text
LName = TextBox3.Text
Rank = TextBox4.Text
myConn = New OleDbConnection("C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2010\\Projects\\Database1.mdf;")
myConn.Open()
sqlString = "INSERT INTO Students (StudentId, FirstName, LastName, Rank) VALUES ('" + SId + "','" + FName + "', '" + LName + "', '" + Rank + "')"
cmd = New OleDbCommand(sqlString, myConn)
cmd.ExecuteNonQuery()
myConn.Close()
End Sub
End Class
I understand that I can call the database for a local machine. Is this the only other method of connecting to the database. Looking to see if there is any other syntax that can be used within Visual Studio Web Developer Express.
Did you use SQL Express when you connect to your database "Database1.mdf"? If so, you could use SqlConnection to connection your SQL Server Express.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myConn As SqlConnection
Dim cmd As SqlCommand
Dim sqlString, SId, FName, LName, Rank As String
SId = TextBox1.Text
FName = TextBox2.Text
LName = TextBox3.Text
Rank = TextBox4.Text
myConn = New SqlConnection("your_connection_string")
myConn.Open()
sqlString = "INSERT INTO Students (StudentId, FirstName, LastName, Rank) VALUES ('" + SId + "','" + FName + "', '" + LName + "', '" + Rank + "')"
cmd = New SqlCommand(sqlString, myConn)
cmd.ExecuteNonQuery()
myConn.Close()
End Sub
End Class
DMK1
Member
7 Points
22 Posts
SQL Express connection to database
May 02, 2012 03:34 AM|LINK
Looking to connect to my database "Database1.mdf" and allow me to add text and insert into my table "Table Name is Students"
The Form has 4 txt boxes that will have the user input data. The code below will hold the data as a string and insert into the students table.
Any help is much appreciated. Language is VB
*************************************************************************************
Imports System.Data.OleDb Public Class _default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myConn As OleDbConnection Dim cmd As OleDbCommand Dim sqlString, SId, FName, LName, Rank As String SId = TextBox1.Text FName = TextBox2.Text LName = TextBox3.Text Rank = TextBox4.Text myConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Database1.mdf;") myConn.Open() sqlString = "INSERT INTO Students (StudentId, FirstName, LastName, Rank) VALUES ('" + SId + "','" + FName + "', '" + LName + "', '" + Rank + "')" cmd = New OleDbCommand(sqlString, myConn) cmd.ExecuteNonQuery() myConn.Close() End Sub End Class********************************************************************
pradeepnt
Member
266 Points
154 Posts
Re: SQL Express connection to database
May 02, 2012 04:25 AM|LINK
Hi....
Imports System.Data.OleDb Public Class _default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myConn As OleDbConnection Dim cmd As OleDbCommand Dim sqlString, SId, FName, LName, Rank As String SId = TextBox1.Text FName = TextBox2.Text LName = TextBox3.Text Rank = TextBox4.Text myConn = New OleDbConnection("C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2010\\Projects\\Database1.mdf;") myConn.Open() sqlString = "INSERT INTO Students (StudentId, FirstName, LastName, Rank) VALUES ('" + SId + "','" + FName + "', '" + LName + "', '" + Rank + "')" cmd = New OleDbCommand(sqlString, myConn) cmd.ExecuteNonQuery() myConn.Close() End Sub End ClassYou can use the address of express database.
Pradeep Narayan
DMK1
Member
7 Points
22 Posts
Re: SQL Express connection to database
May 02, 2012 05:50 PM|LINK
I understand that I can call the database for a local machine. Is this the only other method of connecting to the database. Looking to see if there is any other syntax that can be used within Visual Studio Web Developer Express.
Thanks : )
Chen Yu - MS...
All-Star
21584 Points
2493 Posts
Microsoft
Re: SQL Express connection to database
May 07, 2012 09:25 AM|LINK
Hi DMK1,
Did you use SQL Express when you connect to your database "Database1.mdf"? If so, you could use SqlConnection to connection your SQL Server Express.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myConn As SqlConnection Dim cmd As SqlCommand Dim sqlString, SId, FName, LName, Rank As String SId = TextBox1.Text FName = TextBox2.Text LName = TextBox3.Text Rank = TextBox4.Text myConn = New SqlConnection("your_connection_string") myConn.Open() sqlString = "INSERT INTO Students (StudentId, FirstName, LastName, Rank) VALUES ('" + SId + "','" + FName + "', '" + LName + "', '" + Rank + "')" cmd = New SqlCommand(sqlString, myConn) cmd.ExecuteNonQuery() myConn.Close() End Sub End ClassI found this thread, it may help you.
http://stackoverflow.com/questions/188963/connecting-to-sql-server-with-visual-studio-express-editions
Thanks.
Feedback to us
Develop and promote your apps in Windows Store
DMK1
Member
7 Points
22 Posts
Re: SQL Express connection to database
Jun 08, 2012 05:38 PM|LINK
The answer to my question was to make one of the colums in the database a primary key. Once I made the primary key I was able to edit.
Database Table looked like this below....
id - numchar(10) - Make this the primary key in your database "using 2008 sql server express, simply right click and make primary key"
Fname - varchar (50)
Lname - varchar (50)
Thanks for everyones help.