Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

SQLChars conversion in C#

Back to Forum: Tech Off
  • User profile image
    Shark_M

    Hi guys,
    I have made a class that wraps stored procedures for a database i made,

    now there are methods that return SQLDataReader
    and most methods takes variables that are SQLChars or SQLBooleans

    how do i convert a string to SQLChars?

    for example

    public SqlGuid GetUserIDFromNickname(SqlChars @UserNickanme)
            {
                if (UserNickanme != null)
                {
                    string cmdString = "Exec usp_GetUserIDFromNickname" + "@UserNickname=" + UserNickanme;
                    SqlDataReader tempSqlDatardr = ConnectToSQLAndExecuteQuery(cmdString);
                    if (tempSqlDatardr.HasRows && tempSqlDatardr.RecordsAffected == 1)
                    {
                        while (tempSqlDatardr.Read())
                        {
                            return tempSqlDatardr.GetGuid(0);
                        }
                    }
                    else
                    {
                        return 0;
                    }
                    tempSqlDatardr = null;
                    cmdString = null;
                }
                else
                {
                    return null;
                }
            }

    private static SqlDataReader ConnectToSQLAndExecuteQuery(string cmdString)
            {
                SqlDataReader temDataReader = null;
                if (cmdString != string.Empty)
                {
                    using (SqlConnection SQLdbconn = new SqlConnection(GetConnectionString()))
                    {
                        using (SqlCommand SQLcmd = new SqlCommand(cmdString, SQLdbconn))
                        {
                            SQLcmd.CommandType = System.Data.CommandType.StoredProcedure;
                            SQLdbconn.Open();
                            temDataReader = SQLcmd.ExecuteReader();
                            SQLdbconn.Close();
                            return temDataReader;

                        }
                     
                    }
                    cmdString = string.Empty;
                }
            }

    how do i call this method, with a string?

    GetUserIDFromNickname("Bell")?

    its say cannot do implicit conversion!?

    help me out




  • User profile image
    mot256

    Hi

    Y do u need to use SqlChar in this example? Just use "string" for the parameter...
    Oh yes, and remember to put in a space after usp_GetUserIDFromNickname or u will get an SQL error!

    Francois Nel (aka mot256)

  • User profile image
    mot256

    OK, but let me be fair now...

     

    You could go 2 (maybe more) routes:

     

    1) Create an overloaded method of GetUserIDFromNickname that receives a System.string parameter, and then convert the string parameter with:

    SqlChar sqlcUserNickname = new SqlChar(new SqlString(UserNickanme))

    And then call the original GetUserIDFromNickname by passing sqlcUserNickname

     

    OR

     

    2) You could call GetUserIDFromNickname like this:

    GetUserIDFromNickname(new SqlChar(new SqlString("Bell")))


    PS: I did not test this… so please….

     

    Francois Nel (aka mot256)

  • User profile image
    mot256

    Sorry my "SqlChar" should be "SqlChars"... u c, I told u... Tongue Out

  • User profile image
    Shark_M

    okay,

    I have executed a Stored procedure, using a wrapper class for executing a stored procedure

    i made a method that calls the execution of a given stored procedure

    the stored procedure has output parameters, of about 34 parameters (output),

    I want this method to  return an object that is  filled with a the given parameters for a userid

    so
    i have

    private static Object GetAccountParametersFromUserID(string UserID)
    {
        //execute stored procedure
       
        string _UserName = NameFromStoredProcedure;
        etc.....


       
    }


    after i execute it, how do i return the filled object?

    so i can use the method like this,,

    Console.WriteLine (GetAccountParametersFromUserID (UserID)._UserName);

    thanks for your help

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.