How to Create a SQLCLR project in VB 2005 Express?

Hi all,

I have enabled SQL CLR Integration in my SQL Server 2005 Express. I reasd an article "Managed Data Access Inside SQL Server with ADO.NET ans SQLCLR" by Pablo Castro inhttp://msdn2.microsoft.com/en-us/library/ms345135.aspx that shows how to create a SQLCLR project in the regular Visual Studio: Create a new project => Select the database project category => Select the project type called SQL Server Project. I can not see the above-mentioned items and steps in the VB 2005 Express Edition!!! Is it possible to create a SQLCLR project in VB 2005 Express? If it is possible, please tell me how to create it in VB 2005 Express.

Thanks in advance,

Scott Chang

[875 byte] By [ScottChang] at [2008-1-9]
# 1

It's not possible to connect to a remote server in the VBE designer. I believe it is possible to connect to a local server with the VBE designer.

If you are attempting to connect to a remote SQL Server with VBE that is possible to do with simple code.

ReneeC at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

Hi Scott,

Please check this document SQL Server 2005 Express and remote connection.

Go to: SQL Server 2005 -> Configuration Tools -> SQL Server surface area configuration -> Surface Area Configuration for Services and Connections -> click "Remote Connection" in the tree

You will see this reminder in current window: "By default, SQL Server 2005 Express, Evaluation, and Developer editions allow local client connections only. Enterprise, Standard, and Workgroup editions also listen for remonte client connections over TCP/IP."

You're using the SQL Server 2005 Express!

Best regards,

Martin Xie

MartinXie-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

Hi Martin and ReneeC, Thanks both of you for your responses.

Martin, I read the document SQL Server 2005 Express and remote connect and tried to follow the instructions listed in the document. My Windows XP Pro PC is in my office Network/LAN system. I do not understand the part "restart the database from SQL Management Studio" of the last instuction "Now Apply, Close and restart the database from SQL Management Studio and you're done": My SQL Management Studio is running all the time now and each time I need/(do not need) it, I just click on the "Connect"/(Disconnect) button. How can I restart the database from Management Studio? Please help and enlighen me in this matter by giving me more detaled instructions about "Restarting" the database.

Thanks again,

Scott Chang

ScottChang at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4
Scott Chang wrote:

I do not understand the part "restart the database from SQL Management Studio" of the last instuction "Now Apply, Close and restart the database from SQL Management Studio and you're done"

Hi Scott,

The original content should be "restart the database engine from SQL Management Studio".

In Object Explorer of SQL Server Management Studio, right-click the root node, you will see these menu items: Start, Stop, Pause, Resume and Restart.

MartinXie-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

Hi Martin, Thanks for your valuable response.

I think I restarted the database engine correctly. But I am not sure I got the set-up of remote connection right. So I have the following 2 questions to ask you:

1) In the Surface Area Configuration for Service and Connection - local, I clicked on Remote Connection of Database Engine, SQLEXPRESS and I had a "dot" on "Local and remote connections" and "Using TCP/IP only". Then I clicked on "Apply" and "OK" buttons. Then I went to restart my database engine in SQL Server Management. When I went to check SQL Server 2005 Surface Arera Configuration, I saw the arrow is pointing to "Service", not to "Remote Connections"!!!? Is it right/normal? Please comment this matter.

2) After I restarted the database engine, I executed the following project code (copied from a book) in my VB 2005 Express:

//////////////--Form9.vb/////////////////

mports System.Data.SqlClient

Imports System.Data

Public Class Form9

Dim cnn1 As New SqlConnection

Private Sub Form5_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'Compute top-level project folder and use it as a prefix for

'the primary data file

Dim int1 As Integer = InStr(My.Application.Info.DirectoryPath, "bin\")

Dim strPath As String = Microsoft.VisualBasic.Left(My.Application.Info.DirectoryPath, int1 - 1)

Dim pdbfph As String = strPath & "northwnd.mdf"

Dim cst As String = "Data Source=.\sqlexpress;" & _

"Integrated Security=SSPI;" & _

"AttachDBFileName=" & pdbfph

cnn1.ConnectionString = cst

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Create a command to create a table

Dim cmd1 As New SqlCommand

cmd1.CommandText = "CREATE TABLE FromExcel (" & _

"FirstName nvarchar(15), " & _

"LastName nvarchar(20), " & _

"PersonID int Not Null)"

cmd1.Connection = cnn1

'Invoke the command

Try

cnn1.Open()

cmd1.ExecuteNonQuery()

MessageBox.Show("Command succeeded.", "Outcome", _

MessageBoxButtons.OK, MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(ex.Message)

Finally

cnn1.Close()

End Try

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'Create a command to drop a table

Dim cmd1 As New SqlCommand

cmd1.CommandText = "DROP TABLE FromExcel"

cmd1.Connection = cnn1

'Invoke the command

Try

cnn1.Open()

cmd1.ExecuteNonQuery()

MessageBox.Show("Command succeeded.", "Outcome", _

MessageBoxButtons.OK, MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(ex.Message)

Finally

cnn1.Close()

End Try

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

'Declare FromExcel Data Table and RowForExcel DataRow

Dim FromExcel As New DataTable

Dim RowForExcel As DataRow

FromExcel.Columns.Add("FirstName", GetType(SqlTypes.SqlString))

FromExcel.Columns.Add("LastName", GetType(SqlTypes.SqlString))

FromExcel.Columns.Add("PersonID", GetType(SqlTypes.SqlInt32))

'Create TextFieldParser for CSV file from spreadsheet

Dim crd1 As Microsoft.VisualBasic.FileIO.TextFieldParser

Dim strPath As String = _

Microsoft.VisualBasic.Left( _

My.Application.Info.DirectoryPath, _

InStr(My.Application.Info.DirectoryPath, "bin\") - 1)

crd1 = My.Computer.FileSystem.OpenTextFieldParser _

(My.Computer.FileSystem.CombinePath(strPath, "Book1.csv"))

crd1.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited

crd1.Delimiters = New String() {","}

'Loop through rows of CSV file and populate

'RowForExcel DataRow for adding to FromExcel

'Rows collection

Dim currentRow As String()

Do Until crd1.EndOfData

Try

currentRow = crd1.ReadFields()

Dim currentField As String

Dim int1 As Integer = 1

RowForExcel = FromExcel.NewRow

For Each currentField In currentRow

Select Case int1

Case 1

RowForExcel("FirstName") = currentField

Case 2

RowForExcel("LastName") = currentField

Case 3

RowForExcel("PersonID") = CInt(currentField)

End Select

int1 += 1

Next

int1 = 1

FromExcel.Rows.Add(RowForExcel)

RowForExcel = FromExcel.NewRow

Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException

MsgBox("Line " & ex.Message & _

"is not valid and will be skipped.")

End Try

Loop

'Invoke the WriteToServer method fo the sqc1 SqlBulkCopy

'object to populate FromExcel table in the database with

'the FromExcel DataTable in the project

Try

cnn1.Open()

Using sqc1 As SqlBulkCopy = New SqlBulkCopy(cnn1)

sqc1.DestinationTableName = "dbo.FromExcel"

sqc1.WriteToServer(FromExcel)

End Using

Catch ex As Exception

MessageBox.Show(ex.Message)

Finally

cnn1.Close()

End Try

'Read the FromExcel table and display results in

'a message box

Dim strQuery As String = "SELECT * " & _

"FROM dbo.FromExcel "

Dim str1 As String = ""

Dim cmd1 As New SqlCommand(strQuery, cnn1)

cnn1.Open()

Dim rdr1 As SqlDataReader

rdr1 = cmd1.ExecuteReader()

Try

While rdr1.Read()

str1 += rdr1.GetString(0) & ", " & _

rdr1.GetString(1) & ", " & _

rdr1.GetSqlInt32(2).ToString & ControlChars.CrLf

End While

Finally

rdr1.Close()

cnn1.Close()

End Try

MessageBox.Show(str1, "FromExcel")

End Sub

End Class

//////////////////////////////////////////////////////////////////////////////

I got the following error messages:

SecurityException was unhandled

Request for the permission of type 'System. Security. Permissions.FileIOPermission,mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'failed that is pointing to the code statement "Dim int1 As Integer = InStr (My.Application.Info.DirectoryPath, "bin\")."

Troubleshooting tips:

Store application data in isolated storage.

When deploying an Office solution, check to make sure you have fulfilled all necessary requirments.

Use a certificate to obtain the required permission(s).

If an assembly implementing the custom security references other assemblies, add the referenced assemblies to the full trust assembly list.

Get general help for the exception.

I am a new Microsoft VS.NET Express user to do the SQL Server 2005 Express and VB 2005 Express programming by using the example of a tutorial book. Please help and tell me what is wrong in my SQLCLR sep-up and project coding and how to correct the problem.

Many Thanks in advance,

Scott Chang

ScottChang at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...