创建连接字符串 [英] Create Conection String
问题描述
我想知道是否有人可以帮助我在SQL Server 2008和Visual Studio C#2010之间建立纽带,仅仅是我已经找到了一些教程,但我只是一个初学者,而我如果您能告诉我在哪里可以找到像我这样的人的教程,我会非常烦恼,或者如果您能为我提供帮助,更好的是,我想知道如何开始连接,那很好,谢谢!
Hi, I''d like to know if anyone could help me to create a conection between SQL Server 2008 and Visual Studio C# 2010, is just that i''ve found some tutorials but im just a beginner, and i''m having some troubles to undestand them, if you could tell me where can i find a tutorial for someone like me i would be very thakfull, or if you could help me, better, i want to know how to start the connection and that kind of things, well, thank you!
推荐答案
首次导入命名空间
First Import NameSpace
using System.Data.SqlClient;
并使用以下代码
And Use Following Code
SqlConnection SqlCon = new SqlConnection
("
server=UrSQLserverName;
database=DatabaseName;
uid=SqlUserID;
pwd=SqlPassword
");
然后打开Sql Connection
And Then Open Sql Connection
SqlCon.Open()
然后执行Ur Sql操作....
And Do Ur Sql Operations....
首先,您要做一件事,即从工具箱中将sql dataSource拖到您的表单中,然后配置sqldatasouce,它将导航到某些步骤一旦完成所有步骤,您就可以在Web配置文件中看到连接字符串,如下所示
在webconfig文件中,遵循以下代码:
hi first u do one thing drag the sql dataSource from tool kit once its draged to ur form u configure the sqldatasouce and it will navigate to some steps once u completed all the steps u can see connection string in the web config file as follows
In webconfig file follow the following code:
<connectionstrings>
<add name="EmployeeConnectionString" connectionstring="Data Source=.\SQLEXPRESS;Initial Catalog=dbEmployee;Persist Security Info=True;User ID=a;Password=b">
providerName="System.Data.SqlClient" />
</add></connectionstrings>
这是用于Windows身份验证模式的,一旦找到该副本,就在发明的逗号中按如下所示复制字符串
在课堂上,您可以按照以下方式进行复制
this is for windows authentication mode and once you find this copy the string in the invented commas as follows
in the class u copy as follows
using System.Data;
using System.Data.Sqlclient
class sample
{
protected void page_Load(object sender,EventArgs e)
{
string str="DataSource =the name of server;Initial Catalog=the name of ur database;Integrated Security=True";
try
{
sqlconnection con=new sqlconnection(str);
con.open();//here Connection takes place
string commandText="SQl query";
SqlCommand cmd=new Sqlcommand(commadnText,con);//here the command isintialized
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
lable1.Text=ex.Message;
}
finally
{
con.Close();
}
}
}
试试这个,你可以做到
try this and u can do it
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Text;
使用System.Windows.Forms;
使用System.Data.SqlClient;
命名空间prjConnection
{
公共局部类Form1:Form
{
公共Form1()
{
InitializeComponent();
sqlCon.StateChange + =新的StateChangeEventHandler(sqlCon_StateChange);
sqlCon.InfoMessage + =新的SqlInfoMessageEventHandler(sqlCon_InfoMessage);
}
私有SqlConnection sqlCon =新SqlConnection(数据源=;初始目录=;集成安全性= True");
#地区出口
私有void btnExit_Click(对象发送者,EventArgs e)
{
关闭();
}
#endregion
#地区开放
私有无效btnOpen_Click(对象发送者,EventArgs e)
{
sqlCon.Open();
}
#endregion
#地区关闭
私有无效btnClose_Click(对象发送者,EventArgs e)
{
sqlCon.Close();
}
#endregion
#地区状态
私有void btnState_Click(对象发送者,EventArgs e)
{
txtState.Text = sqlCon.State.ToString();
}
#endregion
#区域连接字符串
私有无效btnconnection_Click(对象发送者,EventArgs e)
{
txtConnectionString.Text = sqlCon.ConnectionString;
}
#endregion
#区域数据包
私有void btnPacket_Click(对象发送者,EventArgs e)
{
txtPacketSize.Text = sqlCon.PacketSize.ToString();
}
#endregion
#区域显示
私有void btnDisplay_Click(对象发送者,EventArgs e)
{
txtConnectionString.Text = sqlCon.ConnectionString;
txtDataSource.Text = sqlCon.DataSource;
txtDatabase.Text = sqlCon.Database;
txtPacketSize.Text = sqlCon.PacketSize.ToString();
txtState.Text = sqlCon.State.ToString();
txtConnectionTimeOut.Text = sqlCon.ConnectionTimeout.ToString();
txtWorkID.Text = sqlCon.WorkstationId;
}
#endregion
#区域状态更改
私有void sqlcon_StateChange(对象发送者,StateChangeEventArgs e)
{
MessageBox.Show(连接状态为:" + e.CurrentState.ToString()+原始状态为:" + e.OriginalState.ToString());
}
#endregion
私有无效sqlCon_InfoMessage(对象发送者,SqlInfoMessageEventArgs e)
{
MessageBox.Show(e.Message);
}
私有void btnGetInfo_Click(对象发送者,EventArgs e)
{
sqlCon.FireInfoMessageEventOnUserErrors = true;
sqlCon.ChangeDatabase(");
}
}
}
如果需要更多帮助,请访问C#中的SQL连接示例.请发送电子邮件dragonsraveolution@thepub.co.za
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace prjConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
sqlCon.StateChange +=new StateChangeEventHandler(sqlCon_StateChange);
sqlCon.InfoMessage +=new SqlInfoMessageEventHandler(sqlCon_InfoMessage);
}
private SqlConnection sqlCon = new SqlConnection("Data Source = ; Initial Catalog = ; Intergrated Security = True ");
# region Exit
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
# endregion
# region open
private void btnOpen_Click(object sender, EventArgs e)
{
sqlCon.Open();
}
# endregion
# region close
private void btnClose_Click(object sender, EventArgs e)
{
sqlCon.Close();
}
# endregion
# region state
private void btnState_Click(object sender, EventArgs e)
{
txtState.Text = sqlCon.State.ToString();
}
# endregion
# region connectionstring
private void btnconnection_Click(object sender, EventArgs e)
{
txtConnectionString.Text = sqlCon.ConnectionString;
}
# endregion
# region packet
private void btnPacket_Click(object sender, EventArgs e)
{
txtPacketSize.Text = sqlCon.PacketSize.ToString();
}
# endregion
# region Display
private void btnDisplay_Click(object sender, EventArgs e)
{
txtConnectionString.Text = sqlCon.ConnectionString;
txtDataSource.Text = sqlCon.DataSource;
txtDatabase.Text = sqlCon.Database;
txtPacketSize.Text = sqlCon.PacketSize.ToString();
txtState.Text = sqlCon.State.ToString();
txtConnectionTimeOut.Text = sqlCon.ConnectionTimeout.ToString();
txtWorkID.Text = sqlCon.WorkstationId;
}
# endregion
# region state change
private void sqlcon_StateChange(object sender, StateChangeEventArgs e)
{
MessageBox.Show("The state of the connection is: " + e.CurrentState.ToString() + "The original state is: " + e.OriginalState.ToString());
}
# endregion
private void sqlCon_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
MessageBox.Show(e.Message);
}
private void btnGetInfo_Click(object sender, EventArgs e)
{
sqlCon.FireInfoMessageEventOnUserErrors = true;
sqlCon.ChangeDatabase("");
}
}
}
just a lil sample of a connection to SQL in C# if you need more help drop an email dragonsraveolution@thepub.co.za
这篇关于创建连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!