<connectionStrings>
<add name="dbConnection" connectionString="Data Source=192.168.1.100Initial Catalog=NanFangMgrUser ID=saPassWord=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
2.在C#中调用:
System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString
3.将上述连接串保存到文本文件中
private string FILE_NAME = Application.StartupPath + "\\mytxtFile.txt"
private void WriteFile(string str)
{
StreamWriter sr
if (File.Exists(FILE_NAME)) //如果文件存在,则创建File.AppendText对象
{
sr = File.AppendText(FILE_NAME)
}
else//如果文件不存在,则创建File.CreateText对象
{
sr = File.CreateText(FILE_NAME)
}
sr.WriteLine(str)
sr.Close()
}
4.从文本文件中去内容
private String ReadTxtFile()
{
if (File.Exists(FILE_NAME)) //如果文件存在
{
String[] strs = System.IO.File.ReadAllLines(FILE_NAME)
return strs[strs.Length - 1]
}
return String.Empty
}
5.数据库连接,并 *** 作
5.1 查询
String ConnectionString=System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString
public DataTable Query(String where)
{
String sql = String.Format("select * from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", "").Replace("insert", "").Replace("", "").Replace("--", "").Replace("exec", ""))
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(ConnectionString))
DataTable dt = new DataTable()
da.Fill(dt)
return dt
}
catch
{
return null
}
}
5.2 新增
public int New(Entities.mytable obj)
{
String sql = "insert into mytable(pkid,a,b,c) values(@pkid,@a,@b,@c)"
SqlConnection cn = new SqlConnection(ConnectionString)
SqlCommand cmd = new SqlCommand(sql, cn)
cmd.Parameters.AddWithValue("@a", obj.a)
cmd.Parameters.AddWithValue("@b", obj.b)
cmd.Parameters.AddWithValue("@c", obj.c)
cmd.Parameters.AddWithValue("@pkid",
String.Empty.Equals(obj.pkid) ? System.Guid.NewGuid().ToString() : obj.pkid)
try
{
if (cn.State != ConnectionState.Open)
cn.Open()
return cmd.ExecuteNonQuery()
}
catch
{
return -1
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close()
}
}
5.3 编辑
public int Update(Entities.mytable obj)
{
String sql = "Update mytable Set a=@a,b=@b,c=@c Where pkid=@ObjectID"
SqlConnection cn = new SqlConnection(ConnectionString)
SqlCommand cmd = new SqlCommand(sql, cn)
cmd.Parameters.AddWithValue("@a", obj.a)
cmd.Parameters.AddWithValue("@b", obj.b)
cmd.Parameters.AddWithValue("@c", obj.c)
cmd.Parameters.AddWithValue("@pkid", obj.pkid)
try
{
if (cn.State != ConnectionState.Open)
cn.Open()
return cmd.ExecuteNonQuery()
}
catch
{
return -1
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close()
}
}
5.4 删除
public int Del(String where)
{
String sql = String.Format("delete from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", ""))
SqlConnection cn = new SqlConnection(ConnectionString)
SqlCommand cmd = new SqlCommand(sql, cn)
try
{
if (cn.State != ConnectionState.Open)
cn.Open()
return cmd.ExecuteNonQuery()
}
catch
{
return -1
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close()
}
}
访问数据库有两种方式:Windows身份验证登录(不需要用户名和密码,适用于访问本地数据库,应用程序和SQL软件安装在同一台服务器)
SQL Server 身份验证登录(适用于访问其他计算机的数据库,当然也可以用这种方式访问本地数据库)
根据楼主的情况应该用 SQL Server 身份验证登录
数据库连接字符串一般写在 connectionStrings节点下,非强制性(如写在appsettings节点下),只不过这样可以用
ConfigurationManager.ConnectionStrings["connString"].ConnectionString
方便访问
以下是笔者的一个 web.config 文件的内容
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connString" connectionString="Data Source=.Initial Catalog=TestDatabaseIntegrated Security=SSPI"/>
</connectionStrings>
</configuration>
附:数据库连接字符串的写法
SQL Server connection strings
SQL ODBC connection strings
Standard Security:<br>"Driver={SQLServer}Server=Your_Server_NameDatabase=Your_Database_NameUid=Your_UsernamePwd=Your_Password"
Trusted connection:<br>"Driver={SQLServer}Server=Your_Server_NameDatabase=Your_Database_NameTrusted_Connection=yes"
SQL OLE DB connection strings
Standard Security:
"Provider=SQLOLEDBData Source=Your_Server_NameInitial Catalog= Your_Database_NameUserId=Your_UsernamePassword=Your_Password"
Trusted connection:
"Provider=SQLOLEDBData Source=Your_Server_NameInitial Catalog=Your_Database_NameIntegrated Security=SSPI"
SQL OleDbConnection .NET strings
Standard Security:
"Provider=SQLOLEDBData Source=Your_Server_NameInitial Catalog= Your_Database_NameUserId=Your_UsernamePassword=Your_Password"
Trusted connection:
"Provider=SQLOLEDBData Source=Your_Server_NameInitial Catalog=Your_Database_NameIntegrated Security=SSPI"
SQL SqlConnection .NET strings
Standard Security:
1. "Data Source=Your_Server_NameInitial Catalog= Your_Database_NameUserId=Your_UsernamePassword=Your_Password" <br>2. "Server=Your_Server_NameDatabase=Your_Database_NameUserID=Your_UsernamePassword=Your_PasswordTrusted_Connection=False"
Trusted connection:
1. "Data Source=Your_Server_NameInitial Catalog=Your_Database_NameIntegrated Security=SSPI"
2."Server=Your_Server_NameDatabase=Your_Database_NameTrusted_Connection=True"
希望回答对你有帮助
具体如下在app.confi中有如下代码, 现在默认的链接数据库时access数据库
<connectionStrings>
<add name="gLocalSqlServer" connectionString="Server=localhost\\SQLExpressIntegrated Security=SSPIdatabase=RIMS" providerName="System.Data.SqlClient"/>
</connectionStrings>
<Att>
<attProvider defaultProvider="AccessAttProvider">
<providers>
<add name="AccessAttProvider" type="R
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)