正确的写连接字符串的方法 [英] correct way of writing connection string

查看:78
本文介绍了正确的写连接字符串的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,但我在连接字符串上输入错误,任何人都可以帮助我...

I'm having the following code but I'm etting an error at the connection string can anybody help me...

SqlConnection con = new SqlConnection();
           DataTable result = new DataTable();
           con.ConnectionString = "Password=Rav1@2o!4;Persist Security Info=True;User ID=fish2014;Initial Catalog=Fish2014;Data Source=Ravidb01\sql2008";
           con.Open();
           String query = "select pwd from TestPractise where uname='" + txtuname.Text + "' and mailid='" + txtmail.Text + "'";
           SqlCommand cmd = new SqlCommand(query, con);
           SqlDataReader dr = cmd.ExecuteReader();
           result.Load(dr);
           var password = Convert.ToString(result.Rows[0]["pwd"]);
           con.Close();

推荐答案

这是因为你有一个反斜杠 \ in它;要么逃避 \\ ,要么使用逐字字符串(在前面放置一个at符号 @ string literal。) http://msdn.microsoft.com/en-us /library/aa691090(v=vs.71).aspx [ ^ ]



除此之外...



0)请使用使用语句来表示Connection,Command和DataReader。

1)请使用参数化查询。

2)演员表演时请不要使用 Convert.ToString

It's because you have a backslash \ in it; either escape it \\ or use a verbatim string (put an at-sign @ on the front of the string literal.) http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx[^]

Other than that...

0) Please use using statements for the Connection, Command, and DataReader.
1) Please use a parameterized query.
2) Please don't use Convert.ToString when a cast will do.
string password = (string) result.Rows[0]["pwd"] ;



3)请不要存储纯文本密码。

4)您不需要DataTable。

5)你有什么,你可以使用ExecuteScalar。

6)你不需要使用 var 知道类型。


这可能对你有帮助....



连接字符串 [ ^ ]
this might help you....

Connction String[^]


SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=ServerName;" +
"Initial Catalog=DataBaseName;" +
"User id=UserName;" +
"Password=Secret;";
conn.Open();


这篇关于正确的写连接字符串的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆