當前位置:編程學習大全網 - 源碼破解 - C#如何實現數據庫連接信息保存在文本中,如何讀取該文本信息進行連接數據庫的操作。 連接數據庫的信息?

C#如何實現數據庫連接信息保存在文本中,如何讀取該文本信息進行連接數據庫的操作。 連接數據庫的信息?

1.數據庫連接:在config文件中的形式

<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();

}

}

  • 上一篇:沒精打采的造句
  • 下一篇:在網店裝修過程中除了使用拍攝的商品照片以外什麽的使用也是必不
  • copyright 2024編程學習大全網