Working with SQL's Text data type

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2004
Posts: 634
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Solved Threads: 7
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Working with SQL's Text data type

 
0
  #1
Aug 31st, 2004
Hey guys. I just want to know how would I write a stored procedure in SQL server 2000 to read, and another to update a Text data type field. I also need to know how I am able to execute this stored Procedure in asp .net using vb .net.

My table is something like this:

ArtID int
Title varchar 200
Contents Text
Hits int
HitText nvarchar 200
UserID nvarchar 20

Any help is appreciated!

Cheers,

Slade
Formerly known as Slade.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 28
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: Working with SQL's Text data type

 
0
  #2
Aug 31st, 2004
Stored Procedure for Reading:
  1. CREATE PROCEDURE dbo.GetArt
  2. AS
  3. Select
  4. *
  5. FROM
  6. TableName

Stored Procedure for Updating:
  1. CREATE PROCEDURE dbo.UpdateArt
  2. {
  3. @ArtID int,
  4. @Title varchar(200),
  5. @Contents text,
  6. @Hits int,
  7. @HitText nvarchar(200),
  8. @UserID nvarchar(20)
  9. }
  10. AS
  11.  
  12. UPDATE
  13. TableName
  14.  
  15. SET
  16. Title = @Title,
  17. Contents = @Contents,
  18. Hits = @Hits,
  19. HitText = @HitText,
  20. UserID = @UserID
  21.  
  22. WHERE
  23. ArtID = @ArtID

Next post will include a class to update .
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 28
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: Working with SQL's Text data type

 
0
  #3
Aug 31st, 2004
Okay, here is a class that will interact with the stored procedures:

  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3.  
  4. Public Class ArtDB
  5.  
  6. Private _connectionString As String
  7.  
  8. Sub New(ByVal ConnectionString As String)
  9. _connectionString = ConnectionString
  10. End Sub
  11.  
  12.  
  13. Public Function GetArt() As SqlDataReader
  14.  
  15. ' Create Instance of Connection and Command Object
  16. Dim myConnection As New SqlConnection(ConnectionString)
  17. Dim myCommand As New SqlCommand("GetAds", myConnection)
  18.  
  19. ' Mark the Command as a SPROC
  20. myCommand.CommandType = CommandType.StoredProcedure
  21.  
  22. ' Execute the command
  23. myConnection.Open()
  24. Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
  25.  
  26. ' Return the datareader
  27. Return result
  28.  
  29. End Function
  30.  
  31.  
  32. Public Sub UpdateArt(ByVal ArtID As Integer, ByVal Title As String, ByVal Contents As String, ByVal Hits As Integer, ByVal HitText As String, ByVal UserID As String)
  33.  
  34. ' Create Instance of Connection and Command Object
  35. Dim myConnection As New SqlConnection(ConnectionString)
  36. Dim myCommand As New SqlCommand("UpdateArt", myConnection)
  37.  
  38. ' Mark the Command as a SPROC
  39. myCommand.CommandType = CommandType.StoredProcedure
  40.  
  41. ' Add Parameters to SPROC
  42. Dim parameterArtID As New SqlParameter("@ArtID", SqlDbType.Int)
  43. parameterArtID.Value = ArtID
  44. myCommand.Parameters.Add(parameterArtID)
  45.  
  46. Dim parameterTitle As New SqlParameter("@Title", SqlDbType.VarChar, 200)
  47. parameterTitle.Value = Title
  48. myCommand.Parameters.Add(parameterTitle)
  49.  
  50. Dim parameterContents As New SqlParameter("@Contents", SqlDbType.Text)
  51. parameterContents.Value = Contents
  52. myCommand.Parameters.Add(parameterContents)
  53.  
  54. Dim parameterHits As New SqlParameter("@Hits", SqlDbType.Int)
  55. parameterHits.Value = Hits
  56. myCommand.Parameters.Add(parameterHits)
  57.  
  58. Dim parameterHitText As New SqlParameter("@HitText", SqlDbType.VarChar, 200)
  59. parameterHitText.Value = HitText
  60. myCommand.Parameters.Add(parameterHitText)
  61.  
  62. Dim parameterUserID As New SqlParameter("@UserID", SqlDbType.VarChar, 20)
  63. parameterUserID.Value = UserID
  64. myCommand.Parameters.Add(parameterUserID)
  65.  
  66. ' Execute the command
  67. myConnection.Open()
  68. myCommand.ExecuteNonQuery()
  69. myConnection.Close()
  70.  
  71. ' Dispose the resource expensive objects
  72. myCommand.Dispose()
  73. myConnection.Dispose()
  74. End Sub
  75.  
  76. Public Property ConnectionString() As String
  77. Get
  78. Return _connectionString
  79. End Get
  80. Set(ByVal Value As String)
  81. _connectionString = Value
  82. End Set
  83. End Property
  84.  
  85. End Class
  86.  

To grab the data using the class:
  1. Dim artData As New ArtDB("ConnectionStringWouldBePlacedHere")
  2. Dim datareader As System.Data.SqlClient.SqlDataReader = artData.GetArt()

To update data using the class:
  1. Dim artData As New ArtDB("ConnectionStringWouldBePlacedHere")
  2. artData.UpdateArt(1, "Title", "Contents", 60, "HitText", "UserID")
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 28
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: Working with SQL's Text data type

 
0
  #4
Aug 31st, 2004
Wow, that was some fun VB.NET practice.. I haven't done anything in VB.NET for a while now
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 634
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Solved Threads: 7
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL's Text data type

 
0
  #5
Aug 31st, 2004
LOL that is basically the standard format I use now, to think it was so simple... and I was just doing stupid things! oh well .


Thanks heaps tekmaven you rule.
Formerly known as Slade.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 28
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: Working with SQL's Text data type

 
0
  #6
Sep 1st, 2004
LOL! Thanks for the praise . Anyways, always try to use n[datatypes]. They might hold 'slightly' less; but they are unicode datatypes (they hold WAY more types of chars).

Also, wherever you're using SQL Queries, try to impliment Stored Procedures. They are faster, and they provide MUCH more security (against nasty SQL injection attacks).
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 634
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Solved Threads: 7
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL's Text data type

 
0
  #7
Sep 1st, 2004
hehe I always use stored procedures. But people shouldn't be able to inject if it's just a reading query right? I'm not too sure. Also, what types of characters WONT text hold? Can you give examples?
Formerly known as Slade.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 634
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Solved Threads: 7
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL's Text data type

 
0
  #8
Sep 1st, 2004
How do I databind the content of the "Content" field to a label? And are you suggesting I use ntext? I don't mind about the size, I was forced to use it because nvarchar was too small since most of it will be for storing html.
Formerly known as Slade.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 634
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Solved Threads: 7
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL's Text data type

 
0
  #9
Sep 1st, 2004
LMAO! I don't know whether to be angry or happy. Turns out I was databinding it correctly but I had the label set to invisible lol.



Cheers Tekmaven, I'll buy you a beer if I ever go to America,

Slade
Formerly known as Slade.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 28
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: Working with SQL's Text data type

 
0
  #10
Sep 1st, 2004
ROFL, I'm underage . I don't think text can store this character: 廙

(FYI, thats a Final Pe in hebrew )

//Edit: I guess MySQL can't store it either .
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC