Suzy:
Yes, you can. Here's the total code. Notice I commented out hte line where
I check for the existence of the table first (you should do the check or
risk throwing an exception, I was just being lazy.) Anyway, if you have a
valid connection string and permissions to create a table from the machine
you are on(if youare using SSPI or if you have a valid username and password
if you arne't using SSPI) then this will do it for you.... HTH,
Bill
Dim sb As New System.Text.StringBuilder
Dim KeepGoing As Boolean = False
Dim cn As New SqlConnection("YourConnectionString")
' sb.Append("if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[FK_Tbl_Invoice_Information_Tbl_Facilities]') and
OBJECTPROPERTY(id, N'IsForeignKey') = 1)")
sb.Append("CREATE TABLE [dbo].[Tbl_FacilitiesTest] (")
sb.Append("[Facility] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL ,")
sb.Append("[Facility_Initials] [nvarchar] (2) COLLATE
SQL_Latin1_General_CP1_CI_AS NOT NULL ,")
sb.Append("[Dept_Number] [int] NOT NULL ,")
sb.Append("[BillingType] [smallint] NOT NULL ,")
sb.Append("[Dictation_Code] [tinyint] NULL ")
sb.Append(") ON [PRIMARY]")
Dim cmd As New SqlCommand(sb.ToString, cn)
Try
If cn.State <> ConnectionState.Open Then cn.Open()
KeepGoing = True
Catch ex As SqlException
Debug.Assert(False, ex.ToString)
End Try
If Not KeepGoing Then
'Alert the user and ask them what they want to do, we cna't
'go any further until we have a good connection. This should
'be in a while loop but I'm lazy this morning..
Else
Try
Dim i As Integer = cmd.ExecuteNonQuery
Catch ex As SqlException
Debug.Assert(False, ex.ToString)
Finally
If cn.State <> ConnectionState.Closed Then cn.Close()
End Try
cn.Dispose()
cmd.Dispose()
End If
suzy said:
Hello,
I understand it is possible to create a SQL server table (with primary keys,
default values, etc) using ado.net/c#.
Can someone point me in the right direction about how to do this please
(with a link to an example etc).
thanks.