我想从表AccountLog中筛选出一些数据的部分字段,然后添加到另一个表AccountLog_bak中做为备份,我的程序如下 ,但是有错误,请求大家帮我看看,或是给出一个更好方案,在下会非常感激!!!!
private void Button4_Click(object sender, System.EventArgs e)
{
string myConn = ConfigurationSettings.AppSettings["connection"];
string mySQLstr = "select AutoNum,AccountID,Income,CreateDate,int(CurrBalance) as Validity,CreateDate+int(CurrBalance) as LastDate from AccountLog a where CreateDate=(select max(CreateDate) from AccountLog where AccountID=a.AccountID) and CreateDate+int(CurrBalance) < now() and int(Income) > 0;"//在表AccountLog中筛选符合条件的记录 OleDbConnection myConnection = new OleDbConnection(myConn);
OleDbCommand myCommand = new OleDbCommand(mySQLstr, myConnection);
//myCommand.Connection.Open();
myConnection.Open();
OleDbDataReader myReader =myCommand.ExecuteReader();
while(myReader.Read())
{
string AutoN = myReader.GetInt32(0).ToString();//该字段本来是自动编号类型
string AccID = myReader.GetValue(1).ToString();//该字段是货币类型
string CreDate = myReader.GetValue(2).ToString();//该字段是日期类型
string Vali = myReader.GetValue(3).ToString();//该字段是货币类型
string LasDate = myReader.GetValue(4).ToString();//该字段是日期类型 string myConn2 = ConfigurationSettings.AppSettings["connection"];
string insertSQLstr = "insert into AccountLog_bak (AutoN_t,AccID_t,CreDate_t,Vali_t,LasDate_t) values (@AutoN,@AccID,@CreDate,@Vali,@LasDate);";
OleDbConnection myConnection2 = new OleDbConnection(myConn2);
OleDbCommand myCommand2 = new OleDbCommand(insertSQLstr, myConnection2);
myCommand2.Connection.Open();
myCommand2.ExecuteNonQuery();
}
myConnection.Close();
}
本人是新手,所以在类型转换时也不能确定用那个转换函数,所以在后面都注明了原来数据的类型。
private void Button4_Click(object sender, System.EventArgs e)
{
string myConn = ConfigurationSettings.AppSettings["connection"];
string mySQLstr = "select AutoNum,AccountID,Income,CreateDate,int(CurrBalance) as Validity,CreateDate+int(CurrBalance) as LastDate from AccountLog a where CreateDate=(select max(CreateDate) from AccountLog where AccountID=a.AccountID) and CreateDate+int(CurrBalance) < now() and int(Income) > 0;"//在表AccountLog中筛选符合条件的记录 OleDbConnection myConnection = new OleDbConnection(myConn);
OleDbCommand myCommand = new OleDbCommand(mySQLstr, myConnection);
//myCommand.Connection.Open();
myConnection.Open();
OleDbDataReader myReader =myCommand.ExecuteReader();
while(myReader.Read())
{
string AutoN = myReader.GetInt32(0).ToString();//该字段本来是自动编号类型
string AccID = myReader.GetValue(1).ToString();//该字段是货币类型
string CreDate = myReader.GetValue(2).ToString();//该字段是日期类型
string Vali = myReader.GetValue(3).ToString();//该字段是货币类型
string LasDate = myReader.GetValue(4).ToString();//该字段是日期类型 string myConn2 = ConfigurationSettings.AppSettings["connection"];
string insertSQLstr = "insert into AccountLog_bak (AutoN_t,AccID_t,CreDate_t,Vali_t,LasDate_t) values (@AutoN,@AccID,@CreDate,@Vali,@LasDate);";
OleDbConnection myConnection2 = new OleDbConnection(myConn2);
OleDbCommand myCommand2 = new OleDbCommand(insertSQLstr, myConnection2);
myCommand2.Connection.Open();
myCommand2.ExecuteNonQuery();
}
myConnection.Close();
}
本人是新手,所以在类型转换时也不能确定用那个转换函数,所以在后面都注明了原来数据的类型。
解决方案 »
- 问一下关于分布式开发的一些问题,高分送上,谢谢!
- javascript学习问题
- DataGrid中使用链接下载,当下载文件是IE能直接读的,要强制下载,求一方法,C#代码。
- 数据库中存有图片地址路径和文字,如何只读出图片的路径?
- ASP.NET中有没有选择文件路径的控件啊?
- 微软msn spaces中的那种照片显示效果是怎么做出来的?有谁知道??讨论一下!!
- 求1工具下载地址,找到后立刻揭帖(在线刷新等答案 谢谢)
- 怎么把别的机子上的ASP.NET web应用程序附加到本地机子上
- 新手在次散50分高手来!DropDownList 利用储存过程存入数据库问题!!!
- 求助!关于TextBox焦点离开后提交的触发
- 请教高手把ASP代码(vb脚本)转为ASP.NET代码(c#脚本)
- 如何保持基于web的datagrid第一列突出显示
但你并没有给myCommand2 添加参数,
不出错反而奇怪了
如果觉得添加参数麻烦就用
string insertSQLstr = "insert into AccountLog_bak (AutoN_t,AccID_t,CreDate_t,Vali_t,LasDate_t) values ("+AutoN +","+AccID +",'"+CreDate +"',"+Vali +",'"+LasDate +"')";
能不能解释一下,直接用values (AutoN ,AccID ,CreDate,Vali,LasDate )可以吗?
这个相当于是字符,你要是不要加号和双引号
就会在数据库里面存在你所打的字符进去
还有 要是整型的话,不需要'"+AccID +"',这样来写 要是字符型才需要的
建议楼主多看一点这方面的书
------------------------------------------------------------------------------------
未指定的错误
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.OleDb.OleDbException: 未指定的错误源错误:
行 134: OleDbCommand myCommand3 = new OleDbCommand(insertSQLstr3, myConnection3);
行 135: //myCommand3.Connection.Open();
行 136: myConnection3.Open();
行 137: myCommand3.ExecuteNonQuery();
行 138:
请问一下这是什么问题呢?