不能使用c#代码和多层体系结构保存数据

本文关键字:体系结构 保存 数据 代码 不能 | 更新日期: 2023-09-27 18:05:09

这段代码最后总是返回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; }
    }
}

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

不能使用c#代码和多层体系结构保存数据

您从未向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();
}