Search code examples
sqlcasp.net-coreblazor-server-side

ODBC connection in a Blazor web app running .net 8 framework


I've very new to web development and never used the new Blazor implementation. I'm trying to set up a web app to connect to a 2000 Microsoft SQL Server. I can't use the standard System.Data.SqlClient as this doesn't support SQL 2000. I believe that I have to use the ODBC connection. Problem is that I cant find any examples out there that have done this. I know it's a lot to ask but would anyone have an example app of this or know where I might find a solution.

The SQL server isn't local and it uses SQL authentication. I'm using Visual Studio 2022, .net 8

Thanks


Solution

  • You could try following:
    Search and Open ODBC Data Source Administrator and confirm the "driver name." enter image description here
    Then you could try with a console app and install package System.Data.Odbc

    OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder
    {
        Driver = "ODBC Driver 17 for SQL Server"
    };
    builder.Add("SERVER", "192.168.2.68");
    builder.Add("Port", "1433");
    builder.Add("DATABASE", "test11");
    builder.Add("UID", "sa");
    builder.Add("PWD", "xxxxxx");
    //builder.Add("TrustServerCertificate", "True");
    
    using (OdbcConnection connection = new OdbcConnection(builder.ConnectionString))
    {
        string sqlQuery = "SELECT * FROM Teams";
        OdbcCommand command = new OdbcCommand(sqlQuery, connection);
        connection.Open();
        OdbcDataReader reader = command.ExecuteReader();
    
        //Print Column Names
        for (int i = 0; i < reader.FieldCount; i++)
        {
            Console.Write(reader.GetName(i) + "\t");
        }
    
        Console.Write("\n");
    
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("{0}\t{1}", reader.GetInt32(0), reader.GetString(1));
            }
        }
    
        reader.Close();
        command.Dispose();
    }
    

    Test result
    enter image description here enter image description here