如何:从服务器资源管理器连接到数据库

如何:从服务器资源管理器连接到数据库,第1张

1数据库连接:在config文件中的形式
<connectionStrings>
<add name="dbConnection" connectionString="Data Source=1921681100;Initial Catalog=NanFangMgr;User ID=sa;PassWord=sa" providerName="SystemDataSqlClient"/>
</connectionStrings>
2在C#中调用:
SystemConfigurationConfigurationManagerConnectionStrings["dbConnection"]ConnectionString
3将上述连接串保存到文本文件
private string FILE_NAME = ApplicationStartupPath + "\\mytxtFiletxt";
private void WriteFile(string str)
{
StreamWriter sr;
if (FileExists(FILE_NAME)) //如果文件存在,则创建FileAppendText对象
{
sr = FileAppendText(FILE_NAME);
}
else //如果文件不存在,则创建FileCreateText对象
{
sr = FileCreateText(FILE_NAME);
}
srWriteLine(str);
srClose();
}
4从文本文件中去内容
private String ReadTxtFile()
{
if (FileExists(FILE_NAME)) //如果文件存在
{
String[] strs = SystemIOFileReadAllLines(FILE_NAME);
return strs[strsLength - 1];
}
return StringEmpty;
}
5数据库连接,并 *** 作
51 查询
String ConnectionString=SystemConfigurationConfigurationManagerConnectionStrings["dbConnection"]ConnectionString;
public DataTable Query(String where)
{
String sql = StringFormat("select from mytable Where {0}", whereToLower()Replace("update", "")Replace("delete", "")Replace("insert", "")Replace(";", "")Replace("--", "")Replace("exec", ""));
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(ConnectionString));
DataTable dt = new DataTable();
daFill(dt);
return dt;
}
catch
{
return null;
}
}
52 新增
public int New(Entitiesmytable 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);
cmdParametersAddWithValue("@a", obja);
cmdParametersAddWithValue("@b", objb);
cmdParametersAddWithValue("@c", objc);
cmdParametersAddWithValue("@pkid",
StringEmptyEquals(objpkid) SystemGuidNewGuid()ToString() : objpkid);
try
{
if (cnState != ConnectionStateOpen)
cnOpen();
return cmdExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cnState != ConnectionStateClosed)
cnClose();
}
}
53 编辑
public int Update(Entitiesmytable 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);
cmdParametersAddWithValue("@a", obja);
cmdParametersAddWithValue("@b", objb);
cmdParametersAddWithValue("@c", objc);
cmdParametersAddWithValue("@pkid", objpkid);
try
{
if (cnState != ConnectionStateOpen)
cnOpen();
return cmdExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cnState != ConnectionStateClosed)
cnClose();
}
}
54 删除
public int Del(String where)
{
String sql = StringFormat("delete from mytable Where {0}", whereToLower()Replace("update", "")Replace("delete", ""));
SqlConnection cn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(sql, cn);
try
{
if (cnState != ConnectionStateOpen)
cnOpen();
return cmdExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
if (cnState != ConnectionStateClosed)
cnClose();
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zz/10487844.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-09
下一篇2023-05-09

发表评论

登录后才能评论

评论列表(0条)

    保存