0% found this document useful (0 votes)
84 views

Using Using Using Using Using Using Public Class Private Private Private Private Private Private Null Public

The document contains code for connecting to a SQL database and executing SQL scripts from files. It includes code to open SQL script files, connect to a database using SqlConnection, create a Server object, and use the Server object's ConnectionContext to execute the SQL scripts. Exceptions are caught for any errors executing the SQL statements.

Uploaded by

eddmadu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Using Using Using Using Using Using Public Class Private Private Private Private Private Private Null Public

The document contains code for connecting to a SQL database and executing SQL scripts from files. It includes code to open SQL script files, connect to a database using SqlConnection, create a Server object, and use the Server object's ConnectionContext to execute the SQL scripts. Exceptions are caught for any errors executing the SQL statements.

Uploaded by

eddmadu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

using using using using using using

System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data.SqlClient;

public class Queries : System.Windows.Forms.Form { private System.Windows.Forms.TextBox txtResult; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button cmdExecute; private System.Windows.Forms.TextBox txtSql; private System.ComponentModel.Container components = null; public Queries() { InitializeComponent(); } private void InitializeComponent() { this.txtSql = new System.Windows.Forms.TextBox (); this.txtResult = new System.Windows.Forms.Text Box(); this.label1 = new System.Windows.Forms.Label() ; this.label2 = new System.Windows.Forms.Label() ; this.cmdExecute = new System.Windows.Forms.But ton(); this.SuspendLayout(); this.txtSql.Location = new System.Drawing.Poin t(0, 32); this.txtSql.Multiline = true; this.txtSql.Name = "txtSql"; this.txtSql.Size = new System.Drawing.Size(400 , 72); this.txtSql.TabIndex = 0;

this.txtSql.Text = ""; this.txtResult.Location = new System.Drawing.P oint(0, 184); this.txtResult.Multiline = true; this.txtResult.Name = "txtResult"; this.txtResult.Size = new System.Drawing.Size( 400, 88); this.txtResult.TabIndex = 1; this.txtResult.Text = ""; this.label1.Font = new System.Drawing.Font("Mi crosoft Sans Serif", 8.25F, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUnit.Point, ((System.Byte) (0))); this.label1.Location = new System.Drawing.Poin t(8, 8); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(384 , 16); this.label1.TabIndex = 2; this.label1.Text = "Type a SQL statement in th e text box."; this.label1.TextAlign = System.Drawing.Content Alignment.MiddleCenter; this.label2.Font = new System.Drawing.Font("Mi crosoft Sans Serif", 8.25F, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUnit.Point, ((System.Byte) (0))); this.label2.Location = new System.Drawing.Poin t(0, 160); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(392 , 16); this.label2.TabIndex = 3; this.label2.Text = "Execution Result"; this.label2.TextAlign = System.Drawing.Content Alignment.MiddleCenter;

this.cmdExecute.Location = new System.Drawing. Point(152, 112); this.cmdExecute.Name = "cmdExecute"; this.cmdExecute.Size = new System.Drawing.Size (104, 32); this.cmdExecute.TabIndex = 4; this.cmdExecute.Text = "Execute Command"; this.cmdExecute.Click += new System.EventHandl er(this.cmdExecute_Click); this.AutoScaleBaseSize = new System.Drawing.Si ze(5, 13); this.ClientSize = new System.Drawing.Size(400, 275); this.Controls.Add(this.cmdExecute); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.txtResult); this.Controls.Add(this.txtSql); this.Name = "Queries"; this.Text = "Tables and Relationships"; this.ResumeLayout(false); } private void cmdExecute_Click(object sender, System .EventArgs e) { try{ SqlConnection conn = new SqlConnection(@"s erver=(local)\SQLEXPRESS;database=MyDatabase;Integrate d Security=SSPI"); conn.Open(); string strSQL=txtSql.Text; SqlCommand cmd= new SqlCommand(strSQL, con n); cmd.ExecuteReader(); conn.Close(); txtResult.Text = "SQL executed successfull

y."; } catch (System.Data.SqlClient.SqlException e x) { txtResult.Text = "There was an error in executing the SQ L. " + "Error Message:" + ex.Message; } } static void Main() { Application.Run(new Queries()); } }
using using using using System.Data.SqlClient; System.IO; Microsoft.SqlServer.Management.Common; Microsoft.SqlServer.Management.Smo;

namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True"; FileInfo file = new FileInfo("C:\\myscript.sql"); string script = file.OpenText().ReadToEnd(); SqlConnection conn = new SqlConnection(sqlConnectionString); Server server = new Server(new ServerConnection(conn)); server.ConnectionContext.ExecuteNonQuery(script); } } }

using using using using using using using using

System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Data; System.Configuration;

using using using using using using using using using

System.Web.Security; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls; System.IO; System.Data.SqlClient; System.Security.Principal; System.Web.Mail; Microsoft.SqlServer.Management.Common; Microsoft.SqlServer.Management.Smo;

namespace ConsoleApplication1 { class Program { static void Main(string[] args) { FileInfo file = new FileInfo(@"\\ares\c$\Inetpub\wwwroot\TestArea\SQL\testsql.sql"); string script = file.OpenText().ReadToEnd(); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WhatIfDatabaseConnectionSt ring"].ConnectionString)) { conn.Open(); SqlCommand sqlComm = new SqlCommand(script, conn); sqlComm.ExecuteNonQuery(); } } }

public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) {

} }

using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common;

3. The hris_db1.sql is the sql script file.


string strConnData = Data Source=.;Initial Catalog=maser;Integrated Security=True; SqlConnection connData = new SqlConnection(strConnData); FileInfo file = new FileInfo(@C:\Databases\hris_db1.sql); string script = file.OpenText().ReadToEnd(); Server server = new Server(new ServerConnection(connData)); server.ConnectionContext.ExecuteNonQuery(script); file.OpenText().Close(); connData.Close();

You might also like