Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2003
    Posts
    30

    Unanswered: Visual Studio NET Connection to MS SQL

    Hi

    iam new into c++ and ms sql. I need to know how i can connect to MS SQL via Visual Studio C++ (NET). Also which is the best way to connect ODBC / ADO

    Please, can anyone help me to get a tutorial or some c++ code ?



    Thanks,
    Indian

  2. #2
    Join Date
    Sep 2003
    Posts
    66
    odbc get a fast connection, I think.

  3. #3
    Join Date
    Sep 2003
    Posts
    522
    depending on what you mean by odbc/ado. data source? that's slow, even with ado. odbc api's? this one is faster than even provider ,but requires good understanding of api calls, data types, and a lot of code, much more than using ado+ole db provider.

  4. #4
    Join Date
    Sep 2003
    Posts
    30
    thx ms_sql_dba


    can you send me or give a link with some examples how i can get connect via c++ to the ms sql server ?

    example, tutorials, links... whatever that helps to get fast into it would be fine

    thx

  5. #5
    Join Date
    Aug 2002
    Location
    SHANGHAI,CHINA
    Posts
    20
    In .NET,there are many ways connect to sqlserver
    1 ADO.NET
    2 ADODB object
    3 ODBC.NET(using odbc driver)

  6. #6
    Join Date
    Sep 2003
    Posts
    66
    can show me a sample code on how to connect the server by "oledb for odbc".
    thanks.

  7. #7
    Join Date
    Aug 2002
    Location
    SHANGHAI,CHINA
    Posts
    20
    ODBC.NET(not supplied in VS.NET 2002)
    Pls download odbc.net from www.microsoft.com and installed it.
    The ODBC .NET Data Provider requires Microsoft Data Access Components (MDAC) 2.6 or later. MDAC 2.7 is recommended.

    U must write using Microsoft.Data.Odbc before

    my sample codes i connect to sybase(but no problem connect to ms sql server because of odbc).

    private void button1_Click(object sender, System.EventArgs e)
    {
    OdbcConnection cn=new OdbcConnection("dsn=emaoe_export;uid=emaoe_export; pwd=emaoe_export");
    OdbcCommand cmd=new OdbcCommand("Select name from dbo.sysobjects where type='u'",cn);
    cn.Open();
    OdbcDataReader rs=cmd.ExecuteReader();
    while(rs.Read())
    {
    listBox1.Items.Add(rs.GetString(0));
    }
    }

  8. #8
    Join Date
    Aug 2002
    Location
    SHANGHAI,CHINA
    Posts
    20

    another method(using adodb object)

    .NET also can using adodb object which is used in vb6 and vc6.
    First u must add microsoft activex data object 2.x library(COM)

    samples:

    private void button2_Click(object sender, System.EventArgs e)
    {
    ADODB.Connection cn=new ADODB.ConnectionClass();
    cn.Open("dsn=emaoe_export","emaoe_export","emaoe_e xport",-1);
    ADODB.Recordset rs=new ADODB.RecordsetClass();
    rs.Open("Select name from dbo.sysobjects where type='u'",cn,ADODB.CursorTypeEnum.adOpenStatic,ADO DB.LockTypeEnum.adLockReadOnly,-1);
    while (!rs.EOF)
    {
    listBox1.Items.Add(rs.Fields["name"].Value.ToString());
    rs.MoveNext();
    }
    rs.Close();
    cn.Close();
    }

    Do u feel so familiarity!!!!

  9. #9
    Join Date
    Sep 2003
    Posts
    66
    is 'oledb for odbc' the same as 'odbc'
    , i think the 'conn' must be define like this
    dim conn as oledb.oledbconnection
    'cause I still want to use ado.net(oledb),not directly use the odbc.
    this provider name in 'data link properties' is 'microsoft oled db provider for odbc dirvers'
    ^^
    Many thanks for help.

  10. #10
    Join Date
    Aug 2002
    Location
    SHANGHAI,CHINA
    Posts
    20
    before .NET. We connect to database using ado.So there is an oledb provider named "oledb provider for odbc".That's will be no differential between writting
    cn.open "dsn=...;uid=...;pwd=..."
    and
    writting
    cn.open "Provider=MSDASQL.1;Password=emaoe_export;Pers ist Security Info=True;User ID=emaoe_export;Data Source=emaoe_export;Initial Catalog=emaoe_export".

    ADO will use the same oledb provider(oledb provider for odbc).

    If you want to use ado.net. U can use System.data.SqlClient.* or System.data.Oledb.*.but it cannot connect to other database(sybase,oracle,etc).

  11. #11
    Join Date
    Sep 2003
    Posts
    30
    many thx wengyan

    i could connect via Dialog to the SQL Server with ODBC.

    Source Code:
    CDatabase db;
    db.OpenEx(NULL, CDatabase::forceOdbcDialog);


    How can i connect directly ?
    Server Name is "Master" usind ODBC and NT Administration rights. Couldn't find it anywhere

    Can you help ?
    Last edited by Indian; 09-25-03 at 10:21.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •