Click to See Complete Forum and Search --> : problems connecting to a server


ross24
08-13-2009, 09:16 PM
Hey within VB I am trying to make a database, fill it with tables and then fill the tables. I am having problems connecting to the server and am really not sure why. I have posted this thread in two areas of this site because I was not sure what was the best area to put it in.

To put in form1.designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form

Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.CreateDBBtn2 = New System.Windows.Forms.Button
Me.CreateTableBtn = New System.Windows.Forms.Button
Me.DataGrid1 = New System.Windows.Forms.DataGridView
Me.AlterTableBtn2 = New System.Windows.Forms.Button
Me.ViewSPBtn = New System.Windows.Forms.Button
Me.ViewViewBtn = New System.Windows.Forms.Button
Me.CreateSPBtn = New System.Windows.Forms.Button
Me.ViewDataBtn = New System.Windows.Forms.Button
Me.DropTableBtn = New System.Windows.Forms.Button
Me.CreateOthersBtn2 = New System.Windows.Forms.Button
Me.CreateViewBtn = New System.Windows.Forms.Button
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'CreateDBBtn2
'
Me.CreateDBBtn2.Location = New System.Drawing.Point(276, 21)
Me.CreateDBBtn2.Name = "CreateDBBtn2"
Me.CreateDBBtn2.Size = New System.Drawing.Size(79, 55)
Me.CreateDBBtn2.TabIndex = 0
Me.CreateDBBtn2.Text = "CreateDBBtn"
Me.CreateDBBtn2.UseVisualStyleBackColor = True
'
'CreateTableBtn
'
Me.CreateTableBtn.Location = New System.Drawing.Point(361, 21)
Me.CreateTableBtn.Name = "CreateTableBtn"
Me.CreateTableBtn.Size = New System.Drawing.Size(73, 55)
Me.CreateTableBtn.TabIndex = 1
Me.CreateTableBtn.Text = "CreateTableBtn"
Me.CreateTableBtn.UseVisualStyleBackColor = True
'
'DataGrid1
'
Me.DataGrid1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DataGrid1.Location = New System.Drawing.Point(12, 21)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(258, 177)
Me.DataGrid1.TabIndex = 2
'
'AlterTableBtn2
'
Me.AlterTableBtn2.Location = New System.Drawing.Point(361, 82)
Me.AlterTableBtn2.Name = "AlterTableBtn2"
Me.AlterTableBtn2.Size = New System.Drawing.Size(73, 55)
Me.AlterTableBtn2.TabIndex = 3
Me.AlterTableBtn2.Text = "AlterTableBtn2"
Me.AlterTableBtn2.UseVisualStyleBackColor = True
'
'ViewSPBtn
'
Me.ViewSPBtn.Location = New System.Drawing.Point(361, 143)
Me.ViewSPBtn.Name = "ViewSPBtn"
Me.ViewSPBtn.Size = New System.Drawing.Size(73, 55)
Me.ViewSPBtn.TabIndex = 4
Me.ViewSPBtn.Text = "ViewSPBtn"
Me.ViewSPBtn.UseVisualStyleBackColor = True
'
'ViewViewBtn
'
Me.ViewViewBtn.Location = New System.Drawing.Point(276, 143)
Me.ViewViewBtn.Name = "ViewViewBtn"
Me.ViewViewBtn.Size = New System.Drawing.Size(79, 55)
Me.ViewViewBtn.TabIndex = 5
Me.ViewViewBtn.Text = "ViewViewBtn"
Me.ViewViewBtn.UseVisualStyleBackColor = True
'
'CreateSPBtn
'
Me.CreateSPBtn.Location = New System.Drawing.Point(276, 82)
Me.CreateSPBtn.Name = "CreateSPBtn"
Me.CreateSPBtn.Size = New System.Drawing.Size(79, 55)
Me.CreateSPBtn.TabIndex = 6
Me.CreateSPBtn.Text = "CreateSPBtn"
Me.CreateSPBtn.UseVisualStyleBackColor = True
'
'ViewDataBtn
'
Me.ViewDataBtn.Location = New System.Drawing.Point(276, 204)
Me.ViewDataBtn.Name = "ViewDataBtn"
Me.ViewDataBtn.Size = New System.Drawing.Size(75, 55)
Me.ViewDataBtn.TabIndex = 7
Me.ViewDataBtn.Text = "ViewDataBtn"
Me.ViewDataBtn.UseVisualStyleBackColor = True
'
'DropTableBtn
'
Me.DropTableBtn.Location = New System.Drawing.Point(361, 210)
Me.DropTableBtn.Name = "DropTableBtn"
Me.DropTableBtn.Size = New System.Drawing.Size(72, 48)
Me.DropTableBtn.TabIndex = 8
Me.DropTableBtn.Text = "DropTableBtn"
Me.DropTableBtn.UseVisualStyleBackColor = True
'
'CreateOthersBtn2
'
Me.CreateOthersBtn2.Location = New System.Drawing.Point(281, 268)
Me.CreateOthersBtn2.Name = "CreateOthersBtn2"
Me.CreateOthersBtn2.Size = New System.Drawing.Size(69, 41)
Me.CreateOthersBtn2.TabIndex = 9
Me.CreateOthersBtn2.Text = "CreateOthersBtn2"
Me.CreateOthersBtn2.UseVisualStyleBackColor = True
'
'CreateViewBtn
'
Me.CreateViewBtn.Location = New System.Drawing.Point(364, 270)
Me.CreateViewBtn.Name = "CreateViewBtn"
Me.CreateViewBtn.Size = New System.Drawing.Size(68, 38)
Me.CreateViewBtn.TabIndex = 10
Me.CreateViewBtn.Text = "CreateViewBtn"
Me.CreateViewBtn.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(539, 373)
Me.Controls.Add(Me.CreateViewBtn)
Me.Controls.Add(Me.CreateOthersBtn2)
Me.Controls.Add(Me.DropTableBtn)
Me.Controls.Add(Me.ViewDataBtn)
Me.Controls.Add(Me.CreateSPBtn)
Me.Controls.Add(Me.ViewViewBtn)
Me.Controls.Add(Me.ViewSPBtn)
Me.Controls.Add(Me.AlterTableBtn2)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.CreateTableBtn)
Me.Controls.Add(Me.CreateDBBtn2)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub
Friend WithEvents CreateDBBtn2 As System.Windows.Forms.Button
Friend WithEvents CreateTableBtn As System.Windows.Forms.Button
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGridView
Friend WithEvents AlterTableBtn2 As System.Windows.Forms.Button
Friend WithEvents ViewSPBtn As System.Windows.Forms.Button
Friend WithEvents ViewViewBtn As System.Windows.Forms.Button
Friend WithEvents CreateSPBtn As System.Windows.Forms.Button
Friend WithEvents ViewDataBtn As System.Windows.Forms.Button
Friend WithEvents DropTableBtn As System.Windows.Forms.Button
Friend WithEvents CreateOthersBtn2 As System.Windows.Forms.Button
Friend WithEvents CreateViewBtn As System.Windows.Forms.Button

End Class

THE MESSAGE BELOW IS THE SECOND PART OF THIS MESSAGE, IT WAS TOO LONG TO PUT IT ALL IN ONE MESSAGE

ross24
08-13-2009, 09:17 PM
Imports System.Data.SqlClient

Public Class Form1

Private sqlconn As New SqlClient.SqlConnection

Private ConnectionString As String = "myServerAddress; " + "Initial Catalog=myDataBase;" + "Integrated Security=SSPI;"
Private reader As SqlClient.SqlDataReader()
Private conn As New SqlConnection

Private cmd As New SqlCommand
Private AlterTableBtn As System.Windows.Forms.Button
Private sql As String = Nothing
Private CreateOthersBtn As System.Windows.Forms.Button
Private button1 As System.Windows.Forms.Button


Private Sub ExecuteSQLStmt(ByVal sql As String)
' Open the connection
If conn.State = ConnectionState.Open Then
conn.Close()
End If
MsgBox("1,2,3")

ConnectionString = "myServerAddress;" + "Initial Catalog=myDataBase;" + "Integrated Security=SSPI;"
conn.ConnectionString = ConnectionString
conn.Open()
cmd = New SqlClient.SqlCommand(sql, conn)
Try
cmd.ExecuteNonQuery()
Catch ae As SqlClient.SqlException
MessageBox.Show(ae.Message.ToString())
End Try
End Sub 'ExecuteSQLStmt


Private Sub CreateTableBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Open the connection
If conn.State = ConnectionState.Open Then
conn.Close()
End If
ConnectionString = "myServerAddress; " + "Initial Catalog=myDataBase;" + "Integrated Security=SSPI;"
conn.ConnectionString = ConnectionString
conn.Open()
sql = "CREATE TABLE myTable" + "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," + "myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)"
cmd = New SqlClient.SqlCommand(sql, conn)
Try
cmd.ExecuteNonQuery()
' Adding records the table
sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1001, 'Puneet Nehra', 'A 449 Sect 19, DELHI', 23.98 ) "
cmd = New SqlClient.SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1002, 'Anoop Singh', 'Lodi Road, DELHI', 353.64) "
cmd = New SqlClient.SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1003, 'Rakesh M', 'Nag Chowk, Jabalpur M.P.', 43.43) "
cmd = New SqlClient.SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " + "VALUES (1004, 'Madan Kesh', '4th Street, Lane 3, DELHI', 23.00) "
cmd = New SqlClient.SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
Catch ae As SqlClient.SqlException
MessageBox.Show(ae.Message.ToString())
End Try
End Sub 'CreateTableBtn_Click



Private Sub CreateSPBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
sql = "CREATE PROCEDURE myProc AS" + " SELECT myName, myAddress FROM myTable GO"
ExecuteSQLStmt(sql)
End Sub 'CreateSPBtn_Click

Private Sub CreateViewBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
sql = "CREATE VIEW myView AS SELECT myName FROM myTable"
ExecuteSQLStmt(sql)
End Sub 'CreateViewBtn_Click

Private Sub CreateOthersBtn2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
sql = "CREATE UNIQUE CLUSTERED INDEX " + "myIdx ON myTable(myName)"
ExecuteSQLStmt(sql)
sql = "CREATE RULE myRule " + "AS @myBalance >= 32 AND @myBalance < 60"
ExecuteSQLStmt(sql)
End Sub 'CreateOthersBtn_Click

Private Sub DropTableBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sql As String = "DROP TABLE MyTable"
ExecuteSQLStmt(sql) '
End Sub 'DropTableBtn_Click


Private Sub ViewDataBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'/ Open the connection
If conn.State = ConnectionState.Open Then
conn.Close()
End If
ConnectionString = "myServerAddress; " + "Initial Catalog=myDataBase;" + "Integrated Security=SSPI;"
conn.ConnectionString = ConnectionString
conn.Open()
' Create a data adapter
Dim da As New SqlClient.SqlDataAdapter("SELECT * FROM myTable", conn)
' Create DataSet, fill it and view in data grid
Dim ds As New DataSet("myTable")
da.Fill(ds, "myTable")
DataGrid1.DataSource = ds.Tables("myTable").DefaultView
End Sub 'ViewDataBtn_Click


Private Sub ViewSPBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'/ Open the connection
If conn.State = ConnectionState.Open Then
conn.Close()
End If
ConnectionString = "myServerAddress; " + "Initial Catalog=myDataBase;" + "Integrated Security=SSPI;"
conn.ConnectionString = ConnectionString
conn.Open()
' Create a data adapter
Dim da As New SqlClient.SqlDataAdapter("myProc", conn)
' Create DataSet, fill it and view in data grid
Dim ds As New DataSet("SP")
da.Fill(ds, "SP")
DataGrid1.DataSource = ds.DefaultViewManager
End Sub 'ViewSPBtn_Click

Private Sub ViewViewBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Open the connection
If conn.State = ConnectionState.Open Then
conn.Close()
End If
ConnectionString = "myServerAddress; " + "Initial Catalog=myDataBase;" + "Integrated Security=SSPI;"
conn.ConnectionString = ConnectionString
conn.Open()
' Create a data adapter
Dim da As New SqlClient.SqlDataAdapter("SELECT * FROM myView", conn)
' Create DataSet, fill it and view in data grid
Dim ds As New DataSet
da.Fill(ds)
DataGrid1.DataSource = ds.DefaultViewManager
End Sub 'ViewViewBtn_Click


Protected Overrides Sub Dispose(ByVal disposing As Boolean)
AppExit()
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Dispose

Private Sub AppExit()
'If Not (reader Is Nothing) Then
' reader.Close()
'End If
If conn.State = ConnectionState.Open Then
conn.Close()
End If
End Sub 'AppExit




Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub DataGrid1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGrid1.CellContentClick

End Sub

Private Sub AlterTableBtn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlterTableBtn2.Click
sql = "ALTER TABLE MyTable ALTER COLUMN" + "myName CHAR(100) NOT NULL"
ExecuteSQLStmt(sql)
End Sub




Private Sub CreateDBBtn2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateDBBtn2.Click
' Create a connection
conn = New SqlConnection(ConnectionString)
' Open the connection



If conn.State <> ConnectionState.Open Then
conn.Open()
End If

Dim sql As String = "CREATE DATABASE mydb ON PRIMARY" + "(Name=test_data, filename = 'C:\mysql\mydb_data.mdf', size=3," + "maxsize=5, filegrowth=10%)log on" + "(name=mydbb_log, filename='C:\mysql\mydb_log.ldf',size=3," + "maxsize=20,filegrowth=1)"
ExecuteSQLStmt(sql)

End Sub
End Class

ross24
08-14-2009, 06:26 PM
Can anyone help? I would be really grateful because I am very confused.