Connect SQL using VB.Net - Page 2


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 33

Thread: Connect SQL using VB.Net

  1. #16
    Join Date
    May 2007
    Posts
    843
    Values are not insert into databse.

  2. #17
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Do you get errors or just no data?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #18
    Join Date
    May 2007
    Posts
    843
    I don't found any error from log file and other also.

    Do you know the location of config of connections tring ?

    A billion thanks for your help.

  4. #19
    Join Date
    May 2007
    Posts
    843
    I cannot connect to the databse yet. I don't found any errors.

  5. #20
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Well, if you can't connect to the database, then not being able to add records makes sense.

    What is the code you are using to do the connection?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  6. #21
    Join Date
    May 2007
    Posts
    843
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient


    Public Class Admin_Login

    Private Sub Admin_Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim mysqlconnection As SqlConnection
    Dim myfirstsqlcommand As SqlCommand
    Dim mysecondsqlcommand As SqlCommand

    Dim mysqlreaqder As SqlDataReader

    'Dim mysqlconnectionDataAdapter As SqlDataAdapter

    mysqlconnection = New SqlConnection("server=localhost;Trusted_Connection=yes;database=VCD")


    If mysqlconnection.State = ConnectionState.Closed Then
    Try
    mysqlconnection.Open()

    myfirstsqlcommand = New SqlCommand("Insert into Admin Table values peter, 1234, _mysqlconnection")

    mysecondsqlcommand = New SqlCommand("select * from Admin Table")

    mysqlreaqder = mysecondsqlcommand.ExecuteReader()

    While mysqlreaqder.Read
    MessageBox.Show("New Row Inserted")

    End While


    Catch ex As Exception
    MessageBox.Show("Cannot connect to database")
    End Try

    If mysqlconnection.State = ConnectionState.Connecting Then
    mysqlconnection.Close()
    End If
    Else
    Me.Close()
    End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Dim userName As String

    userName = TextBox1.Text

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    Dim password As String

    password = TextBox2.Text

    End Sub
    End Class



    THis is the code i use to connect to the databse.

  7. #22
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    And does it work or do you get errors?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  8. #23
    Join Date
    May 2007
    Posts
    843
    There are exception error where i use messagebox to show the error.

    Thanks.

  9. #24
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    What does the exception say?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  10. #25
    Join Date
    May 2007
    Posts
    843
    Inside my exception, i code a messagebox to indicate connection fail. That;s all.

    I using sql server 2005 with sql server management studio express

  11. #26
    Join Date
    May 2007
    Posts
    843
    I uncomment the exception code and i found out an error which is connection property is not initialized.

  12. #27
    Join Date
    May 2007
    Posts
    843
    I also try following code to check my databse.
    databasename = mysqlconnection.DataSource

    MessageBox.Show(databasename)

  13. #28
    Join Date
    May 2007
    Posts
    843
    I can connect to the databse with comment some code after open().


    Below is my code :

    Imports System
    Imports System.Data
    Imports System.Data.SqlClient


    Public Class Admin_Login

    Private Sub Admin_Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim mysqlconnection As SqlConnection
    Dim myfirstsqlcommand As SqlCommand
    Dim mysecondsqlcommand As SqlCommand

    Dim mysqlreaqder As SqlDataReader

    'Dim mysqlconnectionDataAdapter As SqlDataAdapter

    mysqlconnection = New SqlConnection("server=HOME-C5CCA7A162\SQLEXPRESS;Trusted_Connection=yes;database=VCD")


    If mysqlconnection.State = ConnectionState.Closed Then
    Try
    mysqlconnection.Open()
    MessageBox.Show("Connection Successful")


    'myfirstsqlcommand = New SqlCommand("Insert into Admin Table values peter, 1234, mysqlconnection")

    'mysecondsqlcommand = New SqlCommand("select * from Admin Table")

    'mysqlreaqder = mysecondsqlcommand.ExecuteReader()

    'While mysqlreaqder.Read
    'MessageBox.Show("New Row Inserted")

    'End While

    'Me.Hide()
    'Main_Menu.Show()



    Catch ex As Exception
    MessageBox.Show("Cannot connect to database")
    End Try

    If mysqlconnection.State = ConnectionState.Connecting Then
    mysqlconnection.Close()
    End If
    Else
    Me.Close()
    End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Dim userName As String

    userName = TextBox1.Text

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    Dim password As String

    password = TextBox2.Text

    End Sub
    End Class




  14. #29
    Join Date
    May 2007
    Posts
    843
    The problem is 'mysqlreaqder = mysecondsqlcommand.ExecuteReader()

    'While mysqlreaqder.Read
    'MessageBox.Show("New Row Inserted")

    How i can insert data to the database ?

    myfirstsqlcommand = New SqlCommand("Insert into Admin values 'peter', 1234")
    Last edited by Peter_APIIT; 03-10-2008 at 06:15 AM.

  15. #30
    Join Date
    Oct 2004
    Location
    Bangalore, India
    Posts
    47

    Connect SQl server using VB.Net

    Hi,

    Step1: Creating the Database Files

    CREATE DATABASE permission defaults to members of the sysadmin and dbcreator fixed server roles.

    Using Object Explorer, creating a new database requires only that the database name be entered in the new database form. We open the form by right-clicking the databases node under a server and selecting New database. On the ‘General’ page, type a name for the database in the Database Name box. Click Ok. A database with the name you entered will be created.

    Step2: Creating Tables using SQL Server Management Studio

    SQL Server Management Studio provides for creating tables or modify existing ones in a graphical environment.

    1. In the Object Explorer expand the Databases Node and select a database.
    2. To create a New Table right click the Tables node and select New Table from the Menu. Access the Table designer.
    3. Select the Properties Window option in the Menu.
    4. Type Name as Customer and Schema as mySchema.
    5. Rows in the Table view correspond to columns in the Table.
    6. Columns include CustID, CustName, and City.
    7. Column properties listed include Name, Data Type and Allow Nulls.
    8. When you are finished creating the Table, save the table.

    Step4: Connecting to a Database through a VB 2005 program

    The first step to using ADO.NET is to connect to a datasource. Using the Connection object, we instruct ADO.NET the database to contact. The Connection object is the gateway to the database, and all the operations performed against the database must go through this gateway. Connection objects expose a number properties and methods to manipulate from within the code. To do that, type the Imports statement for the System.Data.SqlClient name at the top of the form module.

    Dim connectionString As String = “server=SYS2;” + “integrated security=SSPI;” + “database=FinAccounting”

    The connectionString property is a long string with several attributes separated by semicolons. In the above connectionString, server is the name of the SQL Server, or ‘Local’ if you are running SQL Server on the same machine. We can replace the ‘server’ with the word ‘Data Source’ and ‘database’ with ‘Initial Catalog’. Integrated Security is to specify that the connection should be secure. The possible values for Integrated Security are ‘true’, ‘yes’, and ‘sspi’, which specify a secure connection.
    Last edited by Hack; 03-13-2008 at 07:11 AM.

Similar Threads

  1. find version & service packs
    By rperez in forum Database
    Replies: 5
    Last Post: 01-02-2009, 04:14 PM
  2. Replies: 1
    Last Post: 01-28-2008, 02:18 PM
  3. Connect SQL Server to Hyperion
    By OAP644 in forum Database
    Replies: 1
    Last Post: 06-19-2007, 11:55 AM
  4. Sql Server client not able to connect server
    By Noordeen in forum ASP.NET
    Replies: 0
    Last Post: 08-12-2002, 02:10 AM
  5. Connect SQL Server Box to Oracle Box
    By Mark in forum Database
    Replies: 5
    Last Post: 07-15-2002, 10:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center