求助一C#+SQL Server连接字符串
大家好!C#连接到SQL Server的字符串需要怎么做才能任意连接到SQL Server2000 SQL Server2005 SQL Server2005 Express Edition呢?
大家可以给个例子吗!谢谢! --------------------编程问答-------------------- ..
Server=.;Database=???;UID=???;PWD=???
这还不通用啊. --------------------编程问答--------------------
private string connectionString = "server=aimer\SQLEXPRESS,uid=sa;pwd=;database=db";
public int Excute(string sql, SqlParameter[] values)
{
try
{
sqlConn = new SqlConnection(connectionString);
sqlCmd = new SqlCommand(sql, sqlConn);
sqlCmd.Parameters.Add(values);
sqlConn.Open();
int row = sqlCmd.ExecuteNonQuery();
return row;
}
catch (SqlException ex)
{
throw ex;
return -1;
}
finally
{
sqlConn.Close();
}
}
这是一个简单的方法模型,, --------------------编程问答--------------------
--------------------编程问答-------------------- 同意2楼的做法 --------------------编程问答-------------------- web.config
//数据库连接字符串(web.config来配置)
//<add key="ConnectionString" value="server=127.0.0.1;database=DATABASE;uid=sa;pwd=" /> 这个写在web.config里面
protected static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public static string ConnectionString
{
set { connectionString = value; }
get { return connectionString; }
}
--------------------编程问答--------------------
<connectionStrings>
<add name="ConnectionString" connectionString="server=.\sqlexpress;user=sa;pwd=sa;database=MyOffice;"/>
</connectionStrings>
zgke哥,这个就能通用吗! --------------------编程问答-------------------- 2楼通用。。
"Persist Security Info=False;Integrated Security=true;
Initial Catalog=AdventureWorks;Server=MSSQL1"
"Persist Security Info=False;Integrated Security=SSPI;
database=AdventureWorks;server=(local)"
"Persist Security Info=False;Trusted_Connection=True;
database=AdventureWorks;server=(local)"
3种Windows 身份验证连接到本地服务器
查MSDN上一大把 --------------------编程问答-------------------- string connectionString = "server=aimer\SQLEXPRESS,uid=sa;pwd=;database=db";
public int Excute(string sql, SqlParameter[] values)
{
try
{
sqlConn = new SqlConnection(connectionString);
sqlCmd = new SqlCommand(sql, sqlConn);
sqlCmd.Parameters.Add(values);
sqlConn.Open();
int row = sqlCmd.ExecuteNonQuery();
return row;
}
catch (SqlException ex)
{
throw ex;
return -1;
}
finally
{
sqlConn.Close();
}
}
--------------------编程问答-------------------- 楼上的的都说的还不错了 自己试试 --------------------编程问答-------------------- --------------------编程问答-------------------- Server=IP地址;Database=???;UID=???;PWD=??? 应该够通用了
--------------------编程问答-------------------- 同意5楼的,写在配置文件中 --------------------编程问答-------------------- SqlConnection con = new SqlConnection("server=.;database=xxgl;uid=sa;pwd=");
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select count(*) from YH_YHGL where YHMC=@YHMC and YHMM=@YHMM ";
SqlParameter puid = cmd.Parameters.Add("@YHMC ", SqlDbType.NVarChar);
SqlParameter ppwd = cmd.Parameters.Add("@YHMM ", SqlDbType.NVarChar);
puid.Value = this.textBox1.Text.Trim();
ppwd.Value = this.textBox2.Text.Trim();
con.Open();
int count = (int)cmd.ExecuteScalar();
if (count == 1)
{
MainFrm MainFrm = new MainFrm();
this.Hide();
MainFrm.ShowDialog();
}
else
{
MessageBox.Show("用户编号或口令错误,请核实后重新输入!!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
} --------------------编程问答-------------------- express版的有实例名,不能跟别的通用 --------------------编程问答-------------------- 上面说了那么多,俺就不多说了,呵呵 --------------------编程问答-------------------- 方法不错呀,我也来学一下. --------------------编程问答-------------------- 我都是用一楼的 简单好记 其他的记不到......... --------------------编程问答-------------------- 可以写进配置文件解决 --------------------编程问答-------------------- 关我叼事。。我没事进来干嘛 --------------------编程问答-------------------- 顶一下 --------------------编程问答-------------------- SqlConnection myConnection = new SqlConnection("Data Source= ;initial catalog= ;Trusted_Connection=yes"); --------------------编程问答-------------------- http://www.connectionstrings.com/
所有的连接字符串都在呢 --------------------编程问答-------------------- private string connectionString = "server=aimer\SQLEXPRESS,uid=sa;pwd=;database=db";
--------------------编程问答-------------------- 还不是都是一样的 啊 那只有顶了 --------------------编程问答--------------------
C# codeprivatestringconnectionString="server=aimer\\SQLEXPRESS,uid=sa;pwd=;database=db";publicintExcute(stringsql, SqlParameter[] values) --------------------编程问答-------------------- <add key="DBConnectionString" value="server=(local);database=master;User ID=sa;Password=sa;Connection Reset=FALSE"/> --------------------编程问答-------------------- 3楼写得比较全面啊~~~ --------------------编程问答-------------------- 顶下 --------------------编程问答-------------------- .... --------------------编程问答-------------------- . --------------------编程问答-------------------- 方法挺全的哦
>>>>>>>>>>------------------------------------------------------------------------------------------<<<<<<<<<<
--------------------编程问答--------------------
学习 --------------------编程问答-------------------- rivate string connectionString = "server=aimer\SQLEXPRESS,uid=sa;pwd=;database=db";
网上到处都是 --------------------编程问答-------------------- 配置字符串,可以存到app.config或者web.config中,连接配置,在winform下可用sqlserver2005中带的一个window,web下必须自已写代码做连接。 --------------------编程问答-------------------- 都说的差不多 好像都没有错,顶一下了 --------------------编程问答-------------------- 有好多,网上很多,但是不通用,不过差异也不大,楼上的诸位已经解决了,up一下 --------------------编程问答-------------------- 看一下 学习 --------------------编程问答-------------------- --------------------编程问答-------------------- 是能通用吧 --------------------编程问答-------------------- UP --------------------编程问答--------------------
我手写的时候忘添加 @ 了, 不用帮我强调, 谢谢骚轻... --------------------编程问答-------------------- 最通用的就是sql2000里的写法了
Data Source=服务器名或IP;Initial Catalog=数据库名;User ID=用户名;Password=密码
连2000.2005都没问题
补充:.NET技术 , C#