1.你需要用存储过程,这是典型的脏数据.不过你在同一个线程里循环插入,第二次读到的还是10,这不应该啊,放出代码来看
2.sdr.Read()
sdr是已经读到内存里了,并不是每执行一次Read都重新去数据库里读数据,那么当然不管你插入多少条,使用sdr.Read数据都不会变的
你需要重新连接数据库,重新执行查询,才能查询到最新的数据
2.sdr.Read()
sdr是已经读到内存里了,并不是每执行一次Read都重新去数据库里读数据,那么当然不管你插入多少条,使用sdr.Read数据都不会变的
你需要重新连接数据库,重新执行查询,才能查询到最新的数据
这里的read 只是负责把表名字 提取出来的,然后根据这表名称 插入内容
while (sdr.Read())
{
for (int i = 0; i < 5;i++ )
{
a1 插入5条 //这里的插入内容,也是成功返回1 ,就是你手动打开数据库 在里面查看没有内容
}
} 等5个表都插入完之后再去数据库,查询 才有内容出来
就只放打开数据库,执行SQL语句这些
public void opensql()
{
string fileUrl = Directory.GetCurrentDirectory().ToString() + @"\ku.db3";
if (File.Exists(fileUrl)!=true)//判断数据库文件是否存在
{
SQLiteConnection.CreateFile(fileUrl);//创建数据库文件
}
conn = new SQLiteConnection();
SQLiteConnectionStringBuilder scb = new SQLiteConnectionStringBuilder();
scb.DataSource = fileUrl;
scb.Password = null;
conn.ConnectionString = scb.ToString();
conn.Open();
}
#endregion
#region 写到数据库
public int sqlwirte(string sqlurl, string title, string content, string Introduction, string date)
{ SQLiteCommand cmd = new SQLiteCommand(conn);
cmd.CommandText = "insert into www_" + sqlurl + "(title,content,Introduction,date) values('" + title.Replace("'", "") + "','" + content.Replace("'", "") + "','" + Introduction.Replace("'", "") + "','" + date + "'); ";
return cmd.ExecuteNonQuery();
}
#endregion
第二个问题
gn.opensql(); // 打开数据库 SQLiteDataReader sdr = gn.ReadInfoSql();gn.closesql(); // 关闭数据库
while (sdr.Read()) 这里就出错了 Connection was closed, statement was terminated (翻译:连接关闭,语句已终止)
{
}
为什么要在执行sdr.Read之前执行gn.closesql()??
第一个问题没写gn.closesql()