各位大哥帮忙给看一下以下代码有什么错误
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace zicai
{
public partial class zucai : Form
{
OleDbConnection objConnection;
OleDbCommand sqlcmd1,sqlcmd2;
OleDbCommand sqlInsert,sqlUpdate;
OleDbDataReader reader,reader1;
String dateTime = null;
public zucai()
{
InitializeComponent();
connAccess();
}
private void butStart_Click(object sender, EventArgs e)
{
ReadData();
}
public Boolean connAccess()
{
Boolean status = false;
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=//127.0.0.1//access//zicai.mdb";
try
{
objConnection = new OleDbConnection(strConnection); //建立连接
objConnection.Open(); //打开连接
MessageBox.Show("连接成功");
}catch(Exception ex)
{
MessageBox.Show("Conncection:"+ex.Message);
}
return status;
}
public Boolean ReadData()
{
//String insertStr = null;
Boolean status = false;
//insertStr = @"insert into part2 values()";//sql语句
double id=0,sum=0,count=0,numb=0,num=0;
String factory=null, partNo=null;
try
{
sqlcmd1 = new OleDbCommand(@"select id,,料料,量分配,供量 from part", objConnection);
reader = sqlcmd1.ExecuteReader(); //执行查询
while (reader.Read())
{ //这个read调用很重要!不写的话运行时将提示找不到数据
id =(int) reader.GetValue(0);
factory = reader.GetString(1);
partNo = reader.GetString(2);
sum =reader.GetDouble(3);
num = reader.GetDouble(4);
sqlcmd2 = new OleDbCommand("select id,有效量,回覆交日 from po where ='" + factory + "' and 料='" + partNo + "' and 有效量>0 and rownum=1 order by 回覆交日", objConnection);
reader1 = sqlcmd2.ExecuteReader();
if (reader1.Read())
{
numb =(int) reader1.GetInt32(0);
count = (int)reader1.GetInt32(1);
dateTime = reader1.GetString(2);
}
reader1.Close();
sqlInsert = new OleDbCommand("insert into part2 select * from part where id=" + id + " ", objConnection);
sqlInsert.ExecuteNonQuery();
if (count > sum)
{
sqlUpdate = new OleDbCommand("update part2 set 回覆交日='" + dateTime + "',供量=" + sum + ",status='Y' where id=" + id + "", objConnection);
sqlUpdate.ExecuteNonQuery();
sqlUpdate = new OleDbCommand("update po set 供量=" + (count - sum) + " where id=" + numb + "", objConnection);
sqlUpdate.ExecuteNonQuery();
}
else if (count == sum)
{
sqlUpdate = new OleDbCommand("update part2 set 回覆交日='" + dateTime + "',供量=" + sum + ",status='Y' where id=" + id + "", objConnection);
sqlUpdate.ExecuteNonQuery();
sqlUpdate = new OleDbCommand("update po set 供量=" + (count - sum) + " where id=" + numb + "", objConnection);
sqlUpdate.ExecuteNonQuery();
}
else
{
while (true)
{
sqlcmd2 = new OleDbCommand("select status from part2 where id=" + id + " and status='Y'", objConnection);
reader1 = sqlcmd2.ExecuteReader();
if (!reader1.Read())
break;
else
upDate(num, numb, count, sum, id, factory, partNo);
}
}
/*sqlInsert = new OleDbCommand(@"insert into part2 (ID,,,-次,日,工交日,件位置,料料,"
+ "品名格,存,在,工需求,需求,量分配,回覆交日,供量,料件供源,代_姓名,"
+ "供商代,供商,承,日,L/T,交日期,供商,工日,延,PO_EDC交期,MRP控制_名,完工入,"
+ "工,工件,生量,母工工日,工作中心,工作中心明,工,有料PO之料料,建改(主)替代料)"
+" values("+reader.GetInt32(1)+",'"+reader.GetString(2)+"')", objConnection);*/
}
status = true;
objConnection.Close();
reader.Close();
}catch(Exception ex){
status = false;
MessageBox.Show("Update:"+ex.Message);
}
return status;
}
public void upDate(double num, double numb, double count, double sum, double id, String factory, String partNo)
{
num = sum - count;
try
{
sqlUpdate = new OleDbCommand("update part2 set 回覆交日='" + dateTime + "',供量=" + count + ",status='Y' where id=" + id + "", objConnection);
sqlUpdate.ExecuteNonQuery();
sqlUpdate = new OleDbCommand("update po set 供量=" + 0 + " where id=" + numb + "", objConnection);
sqlUpdate.ExecuteNonQuery();
sqlInsert = new OleDbCommand("insert into part2 as (select * from part where id=" + id + ") ", objConnection);
sqlInsert.ExecuteNonQuery();
sqlcmd2 = new OleDbCommand("select id,有效量 from po where ='" + factory + "' and 料='" + partNo + "' and 有效量>0 and rownum=1 order by 回覆交日", objConnection);
reader1 = sqlcmd2.ExecuteReader();
if (reader1.Read())
{
numb = reader1.GetInt32(1);
count = reader1.GetInt32(2);
dateTime = reader1.GetString(3);
}
reader1.Close();
if (numb > num)
{
sqlUpdate = new OleDbCommand("update part2 set 回覆交日='" + dateTime + "',供量=" + num + ",status='Y' where id=" + id + "", objConnection);
sqlUpdate.ExecuteNonQuery();
sqlUpdate = new OleDbCommand("update po set 供量=" + (numb - num) + " where id=" + numb + "", objConnection);
sqlUpdate.ExecuteNonQuery();
}
else if (numb == num)
{
sqlUpdate = new OleDbCommand("update part2 set 回覆交日='" + dateTime + "',供量=" + sum + ",status='Y' where id=" + id + "", objConnection);
sqlUpdate.ExecuteNonQuery();
sqlUpdate = new OleDbCommand("update po set 供量=" + (numb - num) + " where id=" + numb + "", objConnection);
sqlUpdate.ExecuteNonQuery();
}
else
{
sqlUpdate = new OleDbCommand("update part2 set 回覆交日='" + dateTime + "',供量=" + numb + ",status='Y' where id=" + id + "", objConnection);
sqlUpdate.ExecuteNonQuery();
sqlUpdate = new OleDbCommand("update po set 供量=" + 0 + " where id=" + numb + "", objConnection);
sqlUpdate.ExecuteNonQuery();
}
}catch(Exception ex)
{
MessageBox.Show("Data:"+ex.Message);
}
}
}
}
exception insert string null
[解决办法]
先说有什么错误才好分析。
粗看一下,代码很乱,逻辑很乱,错误是难免的,大概有:
strConnection += @"Data Source=//127.0.0.1//access//zicai.mdb";
访问本机文件干吗用这种形式?直接用路径好了,如果是网络共享路径应该写成:
strConnection += @"Data Source=\\地址\access\zicai.mdb";
@后面的字符串转义字符不起作用,况且你那还不是Windows路径分割符。
sqlUpdate这个创建了无数遍,都看晕了,也没找到关闭的地方,这些需要控制生命周期的变量用using括起来可以简化作用域管理,OleDbCommand类可以通过改变CommandText重用,尽量减少重复创建次数,即提高效率也减少出错机会。
[解决办法]
谁为空值?null吗?
[解决办法]
看看详细异常信息
[解决办法]
写代码时要记得带上必要的注释
[解决办法]
数据分析中.......................
============================================
需求:
A料要30
A料要100
订单:
A料 100,2013/01/10号交货
A料 50 ,2013/02/10号交货
结果:
A,30,2013/01/10号交货
A,70,2013/01/10号交货
A,30,2013/02/10号交货
这个目前这么理解:
第一批要 30
那么 就从订单中取出30 [2013/01/10号交货] 第一批(完成)
第二批要 100
那么 就从订单中取出余下的70(100-30) [2013/01/10号交货] 第二批上
[2013/02/10号交货]得到 50 A料
从中取30 [2013/02/10号交货] 第二批下 (完成)
剩余 20 怎么处理?
===============================================
需求
B料要100
订单:
B料 100,2013/01/10号交货
结果:
B,100,2013/01/10号交货
这个目前这么理解:
第一批 要 100
订单 有 100
[2013/01/10号交货] 第一批 (完成)
剩余 0
===============================================
需求:
C料要150
C料要50
订单:
C料 100,2013/01/10号交货
C料 100,2013/01/10号交货 (不科学 应该是 2013/02/10号交货 不然说不通)
结果:
C,100,2013/01/10号交货
C,50,2013/02/10号交货
C,50,2013/02/10号交货
第一批要 150
取 100 不够 差 50 [2013/01/10号交货] 第一批上
[2013/02/10号交货]到货 100 取 50 [2013/02/10号交货]第一批下
第二批要 50
取 50(100-50) [2013/02/10号交货] 第二批 (完成)
整理数据表:
需求:
第一批
A料要30 B料要100 C料要150
第二批
A料要100 C料要50
订单(库存):
A料 100,2013/01/10号交货
B料 100,2013/01/10号交货
C料 100,2013/01/10号交货
A料 50 ,2013/02/10号交货
C料 100,2013/02/10号交货
楼主要求结果:(批次完成度)
料号, 数量, 交货日期
A, 30, 2013/01/10号交货
B, 100, 2013/01/10号交货
C, 100, 2013/01/10号交货
A, 70, 2013/02/10号交货
A, 30, 2013/02/10号交货
C, 50, 2013/02/10号交货
C, 50, 2013/02/10号交货
大概是这么理解的 = =。
思路帮忙整理了!
通过读数据库 好像没法一次性读出这种数据 应该还有个需求表。
然后通过筛选 和 运算 拼出数据来。
本人能力有限 只能帮到这里了。。。