近来,有个项目出了点问题,需要写个工具。需要用到查数据库,然后做些处理。
将自己写的范例附上,便于今后查阅。
private void recover_patient_id(String id ,ref String strResult)
{
string connStr = "Driver={"; connStr += SdbDriver; connStr += "};";
connStr += "Server="; connStr += sSdbIP; connStr += ";";
connStr += "Uid="; connStr += SdbUser; connStr += ";";
connStr += "Pwd="; connStr += SdbPassword; connStr += ";";
connStr += "Database="; connStr += SdbName; connStr += ";";
connStr += "Port="; connStr += SdbPort; connStr += ";";
OdbcConnection conn_test = new OdbcConnection(connStr);
//检验连接数据库是否成功
try
{
conn_test.Open();
String sql ="select * from temp where id = ?";
OdbcCommand com = new OdbcCommand(sql,conn_test);
com.Parameters.AddWithValue("@id", id);
using (OdbcDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
String str1 = reader.GetString(0);
String str2 = reader.GetString(1);
}
}
catch (OdbcException ex)
{
LogFile.SaveExLog("Error connecting to the server database: " + ex.Message);
}
conn_test.Close();
}