none
Works in C# but not in Visual Basic, I use Visual Studio 2017 RRS feed

  • Question

  • This does not work in Basic

    Public

    ClassForm1  

    PrivateM_cnADONetConnection AsNewOleDb.OleDbConnection()   

    PrivateSubForm1_Load(sender AsObject, e AsEventArgs) HandlesMyBase.Load

            M_cnADONetConnection.ConnectionString =

    "Provider=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Doug\Documents\Visual Studio 2017\Projects\DatabaseTest-3\DatabaseTest-3\Database.mdf;Integrated Security=True"

            M_cnADONetConnection.Open()   

    EndSub

    End

    Class

    This does work in C#

    namespace

    DatabaseTest_3InC

    {

       

    publicpartialclassForm1: Form

        {       

    publicForm1()

            {

                InitializeComponent();

            }

            SqlConnection con =

    newSqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"|DataDirectory|\\DatabaseTest - 3\\Database.mdf\";Integrated Security=True;Connect Timeout=30");

            SqlCommand Cmd;

            SqlDataReader dr;

           

    privatevoidForm1_Load(objectsender, EventArgs e)

            {                  

            }       

    privatevoidGetData()

            {

                con.Open();

               

    stringsyntax = "";

                Cmd =

    newSqlCommand(syntax,con);

                dr = Cmd.ExecuteReader();

            }

        }

    }

    This is the same program in both C# and Visual Basic, but only works in C#.

    Why

    Thanks


    • Edited by doug_army Sunday, May 6, 2018 2:05 AM
    Sunday, May 6, 2018 2:04 AM

All replies

  • Hi doug_army,

    I would suggest that you could use SqlConnection instead of OleDbConnection, like this:

    Imports System.Data.SqlClient
    
    Public Class Form1
    
        Private M_cnADONetConnection As New SqlConnection()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            M_cnADONetConnection.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\Database\NormalTable.mdf;Integrated Security=True"
            M_cnADONetConnection.Open()
    
        End Sub
    End Class

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 7, 2018 2:35 AM
    Moderator