Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to obtain the SQL Edition from the SQL server using the following code, however I can not get past the syntax error near '('. I'm not sure how to code this in C#, works perfect in SQL Studio. Any help greatly appreciated.

<br />
using (SqlConnection connection = new SqlConnection(connectionString))<br />
{<br />
    strSelect = @"SELECT SERVERPROPERTY('edition')";<br />
    SqlCommand command = new SqlCommand(strSelect, connection);<br />
    command.CommandType = CommandType.Text;<br />
    command.Parameters.AddWithValue(@"SERVERPROPERTY('edition')", edition);<br />
    connection.Open();    <br />
    command.ExecuteScalar();<br />
    connection.Close();<br />
}<br />


Thank you,
Glenn
Posted

Hope this[^] helps.
C#
IDataReader cReader = cmd.ExecuteReader();
if (cReader.Read())
{
    string cText = cReader.GetString(0);
}
 
Share this answer
 
v2
This should work:
C#
using (SqlConnection connection = new SqlConnection(connectionString))
{
    var strSelect = @"SELECT SERVERPROPERTY('edition')";
    SqlCommand command = new SqlCommand(strSelect, connection);
    connection.Open();
    var result = command.ExecuteScalar();
    connection.Close();
    MessageBox.Show(result.ToString());
}
 
Share this answer
 
Comments
CHill60 16-Mar-14 13:29pm    
My 5 - I'd focussed on the way the OP had handled parameterized queries and completely missed the fact that 'edition' is in fact the text to pass to the function! :faceslap:
Marcin Kozub 16-Mar-14 13:31pm    
That was first thing I removed from code ;)
Thx for you vote!
Here's what actually works:
<br />
using (SqlConnection connection = new SqlConnection(connectionString))<br />
{<br />
    strCreate = @"SELECT SERVERPROPERTY('edition')";<br />
    SqlCommand command = new SqlCommand(strCreate, connection);<br />
    command.CommandType = CommandType.Text;<br />
    connection.Open();<br />
    IDataReader cReader = command.ExecuteReader();<br />
    if (cReader.Read())<br />
    {<br />
        edition = cReader.GetString(0);<br />
    }<br />
    connection.Close();<br />
}<br />


Thank you everyone for your help...

PS - Solution 1 also works and is what I'm going with.
 
Share this answer
 
v2
Change the strSelect assignment to be
strSelect = @"SELECT SERVERPROPERTY(@edition)";

and the Parameter addition to be
C#
command.Parameters.AddWithValue("edition)", edition);


See here for another example http://www.dotnetperls.com/sqlparameter[^]

You appear to be confusing the @ at the front of strings which means you don't have to escape certain characters with the @ in a parameterized query which shows the placeholder for the parameter

[Edit] = Here's a link about the @ symbol for verbatim strings[^]

NB Although the comments here do apply (re sqlparameter and verbatim strings) the proposed "solution" is not correct - see Solution 1
 
Share this answer
 
v3
Comments
gmhanna 16-Mar-14 14:00pm    
I've coded it like this:

using (SqlConnection connection = new SqlConnection(connectionString))
{
strCreate = @"SELECT SERVERPROPERTY(@edition)";
SqlCommand command = new SqlCommand(strCreate, connection);
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("edition", edition);
connection.Open();
command.ExecuteScalar();
connection.Close();
}

Now I'm getting, SELECT SERVERPROPERTY(@edition) returned: The parameterized query '(@edition nvarchar(4000))SELECT SERVERPROPERTY(@edition)' expect the parameter '@edition', which was not supplied.
gmhanna 16-Mar-14 14:05pm    
I think it's expecting me to pass some value in the variable edition, which is set to null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900