<connectionStrings>
<add name="dbConnection" connectionString="Data Source=192.168.1.100;Initial Catalog=NanFangMgr;User ID=sa;PassWord=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();
}
}