943,864 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 9262
  • C# RSS
Sep 2nd, 2009
0

Convert String to Sql Datatype!!!

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  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]
Reputation Points: 11
Solved Threads: 12
Junior Poster
Alexpap is offline Offline
117 posts
since Sep 2008
Sep 2nd, 2009
0

Re: Convert String to Sql Datatype!!!

Doesn't anyone know how to help me??
Reputation Points: 11
Solved Threads: 12
Junior Poster
Alexpap is offline Offline
117 posts
since Sep 2008
Sep 2nd, 2009
0

Re: Convert String to Sql Datatype!!!

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 .

C# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 3rd, 2009
0

Re: Convert String to Sql Datatype!!!

Click to Expand / Collapse  Quote originally posted by sknake ...
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 .

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 12
Junior Poster
Alexpap is offline Offline
117 posts
since Sep 2008
Sep 3rd, 2009
1

Re: Convert String to Sql Datatype!!!

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 :

C# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Can't open new WindowsForm
Next Thread in C# Forum Timeline: c# and excel: chart ranges





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC