0
我有一个SQL
数据库。使用excel-DNA的单例模式和服务器连接
然后在一个类中我有一个ExcelFunction
:
[ExcelFunction(Description = "fonction de recherche")]
public static double id(string _isin)
{
double res;
res =DBSQL.Instance.getID(_isin);
return res;
}
然后在anoher类我有我的连接和Singleton模式的创建(为了在多线程情况下安全的)。这个想法可能不明确,只是问我,我会试着解释。关键是打开一个连接(使用单例模式),然后执行请求,然后删除单例以关闭连接。
这里是我的代码:
public class DBSQL : iAccesDB1
{
private SqlConnection MaConn = new SqlConnection("sefhgoefhouzeyf");
private static volatile DBSQL instance;
private static object syncRoot = new Object();
private DBSQL() {}
public static DBSQL Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new DBSQL();
}
}
return instance;
}
}
public void Connection()
{
MaConn.Open();
}
public void CloseConnection()
{
MaConn.Close();
}
public double getID(String _isin)
{
SqlDataReader rd;
double res = -9999;
SqlCommand cd = new SqlCommand("select cpn from tD where isin='" + _isin + "'", MaConn);
try
{
rd = cd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
res =double.Parse(rd["cpn"].ToString());
}
}
catch (Exception ex)
{
throw new Exception("1000: " + ex.Message);
}
return res;
}
}
的问题是,这是行不通的 - 在我的Excel单元格中,我有以下:VALUE?