tags:

views:

167

answers:

5

I'm struggling to return a SQL version using C#, I'm new to SQL programming so any help would be great. I'm getting various errors but below is my latest attempt.

private void buttonOK_Click(object sender, System.EventArgs e)
{
    string strSqlVersion = SQLVersion();
    MessageBox.Show(strSqlVersion);
}

private void sqlversion(string sqlver)
{
    OdbcConnection conn = null;
    try
    {
        conn = getConnection(comboBoxDatabase.Text);
        string strSql = "SELECT @@VERSION";
        conn.Open();
        OdbcCommand cmd = new OdbcCommand(strSql, conn);
        string returnvalue = (string)cmd.ExecuteScalar();
        return returnvalue;
    }
    catch (Exception ex){ }
    finally
    {
        conn.Close();
    }
}
+5  A: 

Your method is declared void, change it to String:

private string sqlversion(string sqlver)
{
    OdbcConnection conn = null;
    try
    {
        conn = getConnection(comboBoxDatabase.Text);
        string strSql = "SELECT @@VERSION";
        conn.Open();
        OdbcCommand cmd = new OdbcCommand(strSql, conn);
        string returnvalue = (string)cmd.ExecuteScalar();
        return returnvalue;
    }
    catch (Exception ex){ }
    finally
    {
        conn.Close();
    }
}
Chris Haas
And see @Rubens Farias post about "Using"
Chris Haas
This does not nearly address all of the problems in the code.
md5sum
Hi thanks, When using MessageBox.Show(sqlversion().ToString()); I get the error "No overload for method 'sqlversion' take 0 arguments", where am I going wrong?
Jamie
due to the way the method is declared, it requires a string parameter, so do MessageBox.Show(sqlversion("").ToString()); or removed string sqlver from the declaration
BarrettJ
Also, since you should at that point be returning a string from your `sqlversion` method, you don't need to `.ToString()` it.
md5sum
+3  A: 

What about:

private string SQLVersion()
{
    string version = "";
    try
    {
        using (OdbcConnection cn = new OdbcConnection("..."))
        using (OdbcCommand cm = cn.CreateCommand())
        {
            cn.Open();
            cm.CommandText = "SELECT @@Version";
            version = cm.ExecuteScalar() as string;
        }
    }
    catch (OdbcException) { }
    return version;
}
Rubens Farias
well, I have a lot of answers... you can spare that upvotes if you want =)
Rubens Farias
Thanks Rubens, worked like a charm!
Jamie
+6  A: 

You can do something like:

var dbProviderFactory = DbProviderFactories.GetFactory(ConnectionStringSettings.ProviderName);
conn = dbProviderFactory .CreateConnection();
conn.ConnectionString = ...
conn.Open();
string serverVersion = conn.ServerVersion;

This is database-agnostic.

RedFilter
+1 Perfect solution to the root problem!
p.campbell
+1, I didn't knew that, ty
Rubens Farias
This is just an optimized refactoring of a small piece of the code (the only part of the posted code that actually would work, assuming his getConnection method is sound), and doesn't address any of the actual problems in the code.
md5sum
A: 

Try this:

private void buttonOK_Click(object sender, System.EventArgs e)
{
    string strSqlVersion = sqlversion();
    MessageBox.Show(strSqlVersion);
}

private string sqlversion()
{

    try
    {
        using (OdbcConnection conn = new OdbcConnection(comboBoxDatabase.Text))
        {
            string strSql = "SELECT @@VERSION";
            using (OdbcCommand cmd = new OdbcCommand(strSql, conn))
            {
                conn.Open();
                string returnvalue = Convert.ToString(cmd.ExecuteScalar());
                return returnvalue;
            }
        }
    }
    catch (OdbcException ex){ }
}

C# is a case sensitive language, so I've fixed the casing of your method call. Also, you cannot return a string from a void method. I've also declared your OdbcConnection as new and initialized it with what I presume is an appropriate connection string from a ComboBox.

EDIT: I've also added using blocks to ensure proper disposal of your OdbcCommand and OdbcConnection objects. Your method call to sqlversion also included a parameter, but none was specified, so I've removed the parameter.

EDIT 2: Modified the catch to only handle (and ignore) only an OdbcException.

md5sum
A: 

Another way to do it, which is much shorter, is to use the SMO library like this:

var con = 
    new Microsoft.SqlServer.Management.Common.ServerConnection("MyServerName");
Debug.Print(con.ServerVersion.ToString());

On mine (SQL Server 2005) this prints "9.0".

Kyralessa
This is just an optimized refactoring of a small piece of the code (the only part of the posted code that actually would work, assuming his getConnection method is sound), and doesn't address any of the actual problems in the code.
md5sum
No, it's an alternate solution to what he's trying to do. All you're doing by posting comments like this to every answer but your own is making yourself look like a jackass.
Kyralessa
The problem is that it's an alternate solution to the only part of the OP code that worked... see the OP comment on http://stackoverflow.com/questions/2065605/returning-sql-version-using-c/2065646#2065646 Chris Haas's answer.
md5sum
The guy is trying to get the version of SQL Server. This is code that gets the version of SQL Server. I'm not sure how much simpler I can make it for you, md5sum.
Kyralessa