|
-
Nov 16th, 2016, 07:26 PM
#1
Thread Starter
Addicted Member
[RESOLVED] How to put SQL database on a server and how to access it?
Hi, I have many questions regarding SQL databases. I'm very new to this subject and I need to know how to upload (or create) a database on a server and where I can find the connection string. First of all, a database on a server can be accessed with any program with the correct connection string, right? Also, does it update in real-time? (I'm thinking of using Azure but I don't know the quality of the free trial.) And this is some example code I made to manage tables in a local database. Is there anything wrong with it or anything I may need to add? Like I said, I'm new to this and don't really know where to begin. Thanks in advance!
~Nic
vb Code:
Imports System.Data.SqlClient Public Class Form1 Dim connString As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""MY_LOCAL_FILE_PATH\Database1.mdf"";Integrated Security=True" Dim myConnection As SqlConnection = New SqlConnection(connString) Dim dr As SqlDataReader Private Sub CreateTable(TName As String) myConnection.Open() Dim command As String = $" IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='{TName}' AND xtype='U') CREATE TABLE [dbo].[{TName}] ( intRecID Int IDENTITY(1,1) NOT NULL PRIMARY KEY, FirstName VARCHAR(50) NOT NULL, LastName VARCHAR(50) NOT NULL, Email VARCHAR(50) NULL, Create_Date Smalldatetime DEFAULT GETDATE() )" Dim cmd As SqlCommand = New SqlCommand(command, myConnection) cmd.ExecuteNonQuery() myConnection.Close() End Sub Private Sub AddEntry(TName As String, FName As String, LName As String, Email As String) myConnection.Open() Dim command As String = $"INSERT INTO {TName} (FirstName, LastName, Email) VALUES ('{FName}', '{LName}', '{Email}')" Dim cmd As SqlCommand = New SqlCommand(command, myConnection) cmd.ExecuteNonQuery() myConnection.Close() End Sub Private Function GetEntry(TName As String, Column As String, ColumnValue As String, ColumnNeeded As String) As String myConnection.Open() Dim command As String = $"SELECT {ColumnNeeded} FROM {TName} WHERE ({Column} = '{ColumnValue}')" Dim cmd As SqlCommand = New SqlCommand(command, myConnection) dr = cmd.ExecuteReader Dim str As String = "" While dr.Read str += dr(0).ToString End While myConnection.Close() Return str End Function Private Sub ClearTable(TName As String) myConnection.Open() Dim command As String = $"DELETE FROM {TName}" Dim cmd As SqlCommand = New SqlCommand(command, myConnection) cmd.ExecuteNonQuery() myConnection.Close() End Sub Private Sub DeleteTable(TName As String) myConnection.Open() Dim command As String = $"DROP TABLE {TName}" Dim cmd As SqlCommand = New SqlCommand(command, myConnection) cmd.ExecuteNonQuery() myConnection.Close() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load CreateTable("T1") ClearTable("T1") MsgBox(GetEntry("T1", "FirstName", "Dave", "Email")) ' This retrieves Dave's email address. End Sub End Class
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|