无法使用C#代码和Multitier架构保存数据

时间:2014-05-25 17:08:24

标签: c#

此代码总是返回false:

tblDars tblDrs = new tblDars();
tblDrs.ID = Convert.ToInt32(txt_subjectID.Text);
tblDrs.Name = txt_subject.Text;
tblDrs.Vahed = Convert.ToInt32(txt_units.Text);
if (!Data.insertSubject(tblDrs)) 
{
    // Cannot save data
}       

我使用预先编写的存储过程来保存数据。我有一个名为Database的课程:

public class Database
{

    SqlConnection sc = new SqlConnection(WebConfigurationManager.ConnectionStrings["School"].ConnectionString);
    public Database()
    { }
//begin insertSubject
    public bool insertSubject(tblDars tbldrs)
    {
        sc.Open();
        try
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.Connection = sc;
            command.CommandText = "InsertDars";
            SqlParameter param1 = new SqlParameter("@id", tbldrs.ID);
            SqlParameter param2 = new SqlParameter("@Name", tbldrs.Name);
            SqlParameter param3 = new SqlParameter("@Vahed", tbldrs.Vahed);

            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            sc.Close();
            return false;
        }
        finally
        {
            if (sc.State != ConnectionState.Closed)
            {
                sc.Close();
            }
        }
        return true;
    }
    //end insertSubject
}

此外,tblDars类的代码如下:

public class tblDars
{
    public tblDars()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    private int id;
    private string name;
    private int vahed;

    public int ID
    {
        set { id = value; }
        get { return id; }
    }
    public string Name
    {
        set { name = value; }
        get { return name; }
    }
    public int Vahed
    {
        set { vahed = value; }
        get { return vahed; }
    }
}

我已经尝试使用相同的程序保存另一个表单(包含更多字段)的数据。这是一次失败。问题出在哪儿?

1 个答案:

答案 0 :(得分:1)

您永远不会将参数添加到SQLCommand。假设您的参数有效,您的代码应如下所示:

try
{
    SqlCommand command = new SqlCommand();
    command.CommandType = CommandType.StoredProcedure;
    command.Connection = sc;
    command.CommandText = "InsertDars";
    SqlParameter param1 = new SqlParameter("@id", tbldrs.ID);
    SqlParameter param2 = new SqlParameter("@Name", tbldrs.Name);
    SqlParameter param3 = new SqlParameter("@Vahed", tbldrs.Vahed);

    /*NEW*/
    command.Parameters.Add(param1);
    command.Parameters.Add(param2);
    command.Parameters.Add(param3);

    command.ExecuteNonQuery();
}

或者你可以像这样写:

try
{
    SqlCommand command = new SqlCommand();
    command.CommandType = CommandType.StoredProcedure;
    command.Connection = sc;
    command.CommandText = "InsertDars";

    //watch for appropriate SqlDbType
    //reference: http://msdn.microsoft.com/de-de/library/system.data.sqldbtype(v=vs.110).aspx
    command.Parameters.Add("@id", SqlDbType.Int).Value = tbldrs.ID;
    command.Parameters.Add("@Name", SqlDbType.VarChar).Value = tbldrs.Name;
    command.Parameters.Add("@Vahed", SqlDbType.VarChar).Value = tbldrs.Vahed;

    command.ExecuteNonQuery();
}