myconn.open();
mycomm=new SqlCommand("select max(distinct(bus_year)) as bus_year from t_main_business where bus_year="+dtp1.Value.Year.ToString()+"",myconn.con);
if (mycomm.ExecuteScalar()!=null)
{
Int32 bus_year=(Int32)mycomm.ExecuteScalar()+1;
textBox1.Text=bus_year.ToString();
}
else 
{
textBox1.Text=dtp1.Value.Year.ToString()+"0001";
}
myconn.close();我这样写有什么地方不对吗?请教高手!多谢!!

解决方案 »

  1.   

    第一次执行时没有错误,第二次时(改变dtp1的值)提示如下:
    未处理的“System.InvalidCastException”类型的异常出现在 byg.exe 中。其他信息: 指定的转换无效。即:Int32 bus_year=(Int32)mycomm.ExecuteScalar()+1;
      

  2.   

    mycomm=new SqlCommand("select max(distinct(bus_year)) as bus_year from t_main_business where bus_year="+dtp1.Value.Year.ToString()+"",myconn.con);这句中 myconn.con 是什么意思? 应该是 myconn 吧?
      

  3.   

    bus_year 是什么字段类型??
    datetime ??如果是datetime的话 就必须在两边加上 ‘  ’
      

  4.   

    to:醉猫
     
    myconn 是我写的数据库连接公共类
    执行时没有语法错误。
    不知道我写的对不对?
      

  5.   

    public int ExecSQL(string strSQL)
    {
    LogManager.WriteLog("Execute  " + strSQL);
    try
    {
    SqlCommand cmd = new SqlCommand(strSQL, this.conn);
    if (this.trans != null)
    {
    cmd.Transaction = this.trans;
    }
    cmd.CommandType = CommandType.Text;

    LogManager.WriteLog("Success"); return cmd.ExecuteNonQuery();
    }
    catch( Exception ex )
    {
    LogManager.WriteLog("Failure  " + ex.Message );
    throw ex;
    }
    }
    public void PrintFinished(SqlConnection cn, SqlTransaction trans, string cd_Kozi, string userName, string programID)
    {
    //変数の宣言
    string strSql = "";
    DataSet ds = null;
    DataBase db = new DataBase(cn, trans); strSql += " SELECT ";
    strSql += "   DISTINCT SR_KAN_ADDR ";
    strSql += " FROM ";
    strSql += "   KP_KANBAN ";
    strSql += " WHERE ";
    strSql += "   CD_KOZI = '" + DataBase.SqlItemTextChange(cd_Kozi) + "' "; db.ExecSQL(strSql, "KM_KANBAN", out ds); for (int i = 0; i < ds.Tables["KM_KANBAN"].Rows.Count; i++)
    {
    //SQL文を生成
    strSql = "";
    strSql += " UPDATE KM_KANBAN ";
    strSql += " SET ";
    strSql += " QT_KINKO = QT_KINKO + QT_ISSUE_SHEETS - QT_ISSUE_COMPL, ";
    strSql += " QT_ISSUE_COMPL = QT_ISSUE_SHEETS, "; //更新footerを作成
    strSql += DataBase.GetUpdateFooter(userName,programID); //更新条件
    strSql += " WHERE (SR_KAN_ADDR = " + ds.Tables["KM_KANBAN"].Rows[i]["SR_KAN_ADDR"] + ") ";
    int ret = db.ExecSQL(strSql);
    if (ret == 0)
    {
    throw new ApplicationException("更新が失敗しました。");
    }
    }
    }