As the title says, how can i convert a string value to a Microsoft.SqlServer.Managment.Smo.Datatype ??? :-/

Example of code that im looking for:

Using Microsoft.Sqlserver.Managment.Smo;

public partial class 'FormName'
{
    String datatype = "Nvarchar";

    Microsoft.SqlServer.Managment.Smo.Datatype dt = datatype;
   
  }

Doesn't anyone know how to help me?? :S

Yes but this is the wrong forum for C# questions, this is the SQL forum :)
Please use the C# forum in the future but don't open a new thread for this question, it will be moved shortly!

To answer your question -- you don't want to do whatever you're attempting. The CLR and MSSQL driver handles the data conversion for. The SQL Data types are listed as an enumeration member. See System.Data.SqlDbType .

using System;

namespace System.Data
{
  public enum SqlDbType
  {
    BigInt = 0,
    Binary = 1,
    Bit = 2,
    Char = 3,
    DateTime = 4,
    Decimal = 5,
    Float = 6,
    Image = 7,
    Int = 8,
    Money = 9,
    NChar = 10,
    NText = 11,
    NVarChar = 12,
    Real = 13,
    UniqueIdentifier = 14,
    SmallDateTime = 15,
    SmallInt = 16,
    SmallMoney = 17,
    Text = 18,
    Timestamp = 19,
    TinyInt = 20,
    VarBinary = 21,
    VarChar = 22,
    Variant = 23,
    Xml = 25,
    Udt = 29,
    Structured = 30,
    Date = 31,
    Time = 32,
    DateTime2 = 33,
    DateTimeOffset = 34,
  }
}

If this is not what you are wanting to do then please clarify what you are trying to accomplish.

Yes but this is the wrong forum for C# questions, this is the SQL forum :)
Please use the C# forum in the future but don't open a new thread for this question, it will be moved shortly!

To answer your question -- you don't want to do whatever you're attempting. The CLR and MSSQL driver handles the data conversion for. The SQL Data types are listed as an enumeration member. See System.Data.SqlDbType .

using System;

namespace System.Data
{
  public enum SqlDbType
  {
    BigInt = 0,
    Binary = 1,
    Bit = 2,
    Char = 3,
    DateTime = 4,
    Decimal = 5,
    Float = 6,
    Image = 7,
    Int = 8,
    Money = 9,
    NChar = 10,
    NText = 11,
    NVarChar = 12,
    Real = 13,
    UniqueIdentifier = 14,
    SmallDateTime = 15,
    SmallInt = 16,
    SmallMoney = 17,
    Text = 18,
    Timestamp = 19,
    TinyInt = 20,
    VarBinary = 21,
    VarChar = 22,
    Variant = 23,
    Xml = 25,
    Udt = 29,
    Structured = 30,
    Date = 31,
    Time = 32,
    DateTime2 = 33,
    DateTimeOffset = 34,
  }
}

If this is not what you are wanting to do then please clarify what you are trying to accomplish.

I thought that because the subject is relavant to SQL, i should create the new thread to Sql section. With regard tothe solution you gave me, i will try it and i will let you know. :D

OK -- just so you know the .NET Framework converts the data types for you. Here is an example SQL call that brings a varchar() back from the SQL Server in the form of a C# string :

private static DataTable LookupUser(string Username)
    {
      /*
       * The reason I return a datatable here is so you can also bring back the user's full
       * name, email address, security rights in the application, etc. I have a "User" class
       * where I defined meta information for users.
       */ 
      const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
      const string query = "Select Password From UserTable (NOLOCK) Where UserName = @UserName";
      DataTable result = new DataTable();
      using (SqlConnection conn = new SqlConnection(connStr))
      {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
          cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username;
          using (SqlDataReader dr = cmd.ExecuteReader())
          {
            result.Load(dr);
            string textBoxStuff = Convert.ToString(result.Rows[0]["Column"]);
          }
        }
      }
      return result;
    }
commented: N.A +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.