Edate和Ldate是TextBox(時間),AllowLogin是CheckBox(True/False)
public bool DepInsert(string EDate,string LDate,int AllowLogin)<這個是類。。>
{
SqlConnection myConn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString"));
SqlCommand myComm=new SqlCommand("EmpInsert",myConn);
myComm.CommandType=CommandType.StoredProcedure;SqlParameter paraEDate=new SqlParameter("@EDate",SqlDbType.DateTime,8);
paraEDate.Value=EDate;
myComm.Parameters.Add(paraEDate);SqlParameter paraLDate=new SqlParameter("@LDate",SqlDbType.DateTime,8);
paraLDate.Value=LDate;
myComm.Parameters.Add(paraLDate);SqlParameter paraAllowLogin=new SqlParameter("@AllowLogin",SqlDbType.Int,4);
paraAllowLogin.Value=AllowLogin;
myComm.Parameters.Add(paraAllowLogin); myConn.Open();
int result=myComm.ExecuteNonQuery();
myConn.Close(); if(result>0)
{return true;}
else
{return false;}
}葉面插入按鈕:
private void btnAdd_Click(object sender, System.EventArgs e)
{
if(tbEmpName.Text.ToString()!=""&&tbLoginId.Text.ToString()!="")
{
bool result=new UserDB().DepInsert             
                                    ((Edate.Text.ToString(),LDate.ToString(),
Convert.ToInt32(cbAllowLogin.Checked));

if(result==true)
{JScript.Alert("新增數據成功!");}
else
{JScript.Alert("新增數據失敗!");}
}如何向數據庫插入時間和True/False.
類中的問題:
public bool DepInsert(string EDate,string LDate,int AllowLogin)時間參數還是用string嗎?
new SqlParameter("@EDate",SqlDbType.DateTime,8);
new SqlParameter("@LDate",SqlDbType.DateTime,8);插入的數據參數類型是這樣的嗎?
new SqlParameter("@AllowLogin",SqlDbType.Int,4);CheckBox插入是Int嗎?葉面插入按鈕:
bool result=new UserDB().DepInsert             
((Edate.Text.ToString(),LDate.ToString(),Convert.ToInt32(cbAllowLogin.Checked));
兩個TextBox(Edate/Ldate)的時間輸入和CheckBox(cbAllowLogin)是否正確。請高手指出錯誤,詳細敍述一下,並可否更正以下。

解决方案 »

  1.   

    1.这句错误int result=myComm.ExecuteNonQuery();
    如果你想得到执行完了的此条记录的id值,你应该写为下面这条语句
    object o=cmd.ExecuteScalar();
    int i=int.Parse(o.ToString());
    2.向数据库中插入时间的话,最好还是用时间格式的,如
    将传过来的值string EDate,string LDate
    再重新赋值一下
    DataTime myEDate=Convert.ToDateTime(EDate);
    DataTime myLDate=Convert.ToDateTime(LDate);
    3.如果你想向数据库中存ture/false
    那么你应该写把传过来的int AllowLogin转化一下
    bool myAllow;
    if(AllowLogin=="0")
    myAllow=true;
    else
    myAllow=false;