我正在尝试调用此函数:
public Exception SetData(string Data)
{
public string conectionstring = "Data Source=MyData1.sdf;Encrypt Database=True;Password=as!;File Mode=Read Write; persist Security Info=False;LCID= 1033";
query = "Insert into [CopyData] values (@data)";
try
{
con = new SqlCeConnection(conectionstring);
com = new SqlCeCommand(query, con);
com.Parameters.AddWithValue("@data",Data);con.Open();
com.ExecuteNonQuery();
con.Close();
return null;
}
catch (Exception ex)
{
return ex;
}
}
通过Main()
使用代码
class Program
{
static void Main(string[] args)
{
DB cdb = new DB();
string b = "hello";
Exception e = cdb.SetData(b);
Console.WriteLine(e);
Console.ReadLine();
}
}
但我得到了这个例外
System.Data.SqlServerCe.SqlCeInvalidDatabaseFormatException
数据库文件是由早期版本的 SQL Server Compact 创建的。请使用 SqlCeEngine.Upgrade() 升级
我尝试通过调用来解决SqlCeEngine.Upgrade()
它
public Exception SetData(string Data)
{
public string conectionstring = "Data Source=MyData1.sdf;Encrypt Database=True;Password=as!;File Mode=Read Write; persist Security Info=False;LCID= 1033";
public string conectionstringc = "Data Source=MyData1.sdf;Encrypt Database=True;Password=as!;File Mode=Read Write; persist Security Info=False;LCID= 1033;Case Sensitive=true";
query = "Insert into [CopyData] values (@data)";
try
{
SqlCeEngine engine = new SqlCeEngine(conectionstring);
engine.Upgrade(conectionstringc);
con = new SqlCeConnection(conectionstring);
com = new SqlCeCommand(query, con);
com.Parameters.AddWithValue("@data",Data);con.Open();
com.ExecuteNonQuery();
con.Close();
return null;
}
catch (Exception ex)
{
return ex;
}
}
但是后来出现了这个异常
System.Data.SqlServerCe.SqlCeException
不需要升级数据库,数据库已经是兼容的格式
我究竟做错了什么?