在数据库中插入值

时间:2015-01-27 11:36:49

标签: c# asp.net oledb

我试图在我的数据库中给出seconden(秒)起始值0。当我插入一行时会发生这种情况,因此秒数设置为0。

这就是我的尝试:

string connect = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|Project.mdb";
        string SqlString = "Insert Into App (Naam, Site, Plaatje, Seconden) Values (?,?,?,0)";
        try
        {
            using (OleDbConnection conn = new OleDbConnection(connect))
            {
                using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("Naam", Voernaamin.Text);
                    cmd.Parameters.AddWithValue("Site", Voersitein.Text);
                    cmd.Parameters.AddWithValue("Plaatje", Voerplaatjein.Text);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    Response.Redirect("Ingelogd2.aspx");
                }
            }

并尝试这样做:(也没有用)

int nul = 0;
        string connect = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|Project.mdb";
        string SqlString = "Insert Into App (Naam, Site, Plaatje, Seconden) Values (?,?,?,?)";
        try
        {
            using (OleDbConnection conn = new OleDbConnection(connect))
            {
                using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("Naam", Voernaamin.Text);
                    cmd.Parameters.AddWithValue("Site", Voersitein.Text);
                    cmd.Parameters.AddWithValue("Plaatje", Voerplaatjein.Text);
                    cmd.Parameters.AddWithValue("Seconden", nul);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    Response.Redirect("Ingelogd2.aspx");
                }
            }

有人可以帮助我,所以我可以Seconden为0吗?它是数据库本身的数字btw。

1 个答案:

答案 0 :(得分:1)

试试这个..

int nul = 0;
    string connect = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|Project.mdb";
    string SqlString = "Insert Into App (Naam, Site, Plaatje, Seconden) Values (@Naam,@Site,@Plaatje,@Seconden)";
    try
    {
        using (OleDbConnection conn = new OleDbConnection(connect))
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Naam", Voernaamin.Text);
                cmd.Parameters.AddWithValue("@Site", Voersitein.Text);
                cmd.Parameters.AddWithValue("@Plaatje", Voerplaatjein.Text);
                cmd.Parameters.AddWithValue("@Seconden", "0");
                conn.Open();
                cmd.ExecuteNonQuery();
                Response.Redirect("Ingelogd2.aspx");
            }
        }