Convert String to Sql Datatype!!!

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 36
Reputation: Alexpap is an unknown quantity at this point 
Solved Threads: 0
Alexpap Alexpap is offline Offline
Light Poster

Convert String to Sql Datatype!!!

 
0
  #1
Sep 2nd, 2009
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:
  1. Using Microsoft.Sqlserver.Managment.Smo;
  2.  
  3. public partial class 'FormName'
  4. {
  5. String datatype = "Nvarchar";
  6.  
  7. Microsoft.SqlServer.Managment.Smo.Datatype dt = datatype;
  8.  
  9. }
Last edited by peter_budo; Sep 3rd, 2009 at 4:28 am. Reason: Adding missing [/code]
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: Alexpap is an unknown quantity at this point 
Solved Threads: 0
Alexpap Alexpap is offline Offline
Light Poster

Re: Convert String to Sql Datatype!!!

 
0
  #2
Sep 2nd, 2009
Doesn't anyone know how to help me??
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Convert String to Sql Datatype!!!

 
0
  #3
Sep 2nd, 2009
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 .

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

If this is not what you are wanting to do then please clarify what you are trying to accomplish.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: Alexpap is an unknown quantity at this point 
Solved Threads: 0
Alexpap Alexpap is offline Offline
Light Poster

Re: Convert String to Sql Datatype!!!

 
0
  #4
Sep 3rd, 2009
Originally Posted by sknake View Post
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 .

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

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Convert String to Sql Datatype!!!

 
1
  #5
Sep 3rd, 2009
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 :

  1. private static DataTable LookupUser(string Username)
  2. {
  3. /*
  4.   * The reason I return a datatable here is so you can also bring back the user's full
  5.   * name, email address, security rights in the application, etc. I have a "User" class
  6.   * where I defined meta information for users.
  7.   */
  8. const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
  9. const string query = "Select Password From UserTable (NOLOCK) Where UserName = @UserName";
  10. DataTable result = new DataTable();
  11. using (SqlConnection conn = new SqlConnection(connStr))
  12. {
  13. conn.Open();
  14. using (SqlCommand cmd = new SqlCommand(query, conn))
  15. {
  16. cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username;
  17. using (SqlDataReader dr = cmd.ExecuteReader())
  18. {
  19. result.Load(dr);
  20. string textBoxStuff = Convert.ToString(result.Rows[0]["Column"]);
  21. }
  22. }
  23. }
  24. return result;
  25. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC