在Windows窗体中将NULL值插入mdf表

时间:2013-04-17 02:04:02

标签: c#

我无法弄清楚我在这里做错了什么,在调试器中,当我设置断点时,字符串都包含字符串值,但是当需要将它们插入表中时,没有任何内容被放入。

该表定义如下

CREATE TABLE [dbo].[tblMissEvents] (
[EventID]          INT            IDENTITY (1, 1) NOT NULL,
[EventTime]        NVARCHAR (MAX) NULL,
[MIS_Event_Action] VARCHAR (50)   NULL,
[Bogey_Type]       VARCHAR (50)   NULL,
PRIMARY KEY CLUSTERED ([EventID] ASC)

);

我的代码:

        string date = DateTime.Now.ToString();
        string MISaction = text1.Text;
        string BogeyType = text2.Text;

        try
        {
            con = new SqlConnection();
            con.ConnectionString =
                @"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\MissileEvents.mdf;integrated security=True;MultipleActiveResultSets=True;Connect Timeout=30";
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText =
                "INSERT into tblMissEvents (EventTime, MIS_Event_Action, Bogey_Type) Values (@date, @MISaction, @BogeyType)";  
            cmd.Parameters.AddWithValue("@date", date);
            cmd.Parameters.AddWithValue("@MISaction", MISaction);
            cmd.Parameters.AddWithValue("@BogeyType", BogeyType);
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        con.Close();

有什么想法?我没有例外,只是没有进入我的表格。

编辑Arshad:

        con = new SqlConnection();
        con.ConnectionString =
            @"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\MissileEvents.mdf;integrated security=True;MultipleActiveResultSets=True;Connect Timeout=30";
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText =
            "INSERT into tblMissEvents (MIS_Event_Action, Bogey_Type) Values (@date, @MISaction, @BogeyType)";  
        cmd.Parameters.AddWithValue("@date", date);
        cmd.Parameters.AddWithValue("@MISaction", MISaction);
        cmd.Parameters.AddWithValue("@BogeyType", BogeyType);
        cmd.Connection = con;
        cmd.ExecuteNonQuery();

更新::代码很好,它正在我的bin \ Debug目录中制作数据库的副本,那就是所有数据的去处。可能是我的连接字符串设置的东西。谢谢大家!

1 个答案:

答案 0 :(得分:0)

您也可以将null值传递为:

 cmd.CommandText =
            "INSERT into tblMissEvents (EventTime, MIS_Event_Action, Bogey_Type)
                                Values (@eventTime, @MISaction, @BogeyType)";  
        cmd.Parameters.AddWithValue("@eventTime", DBNull.Value);          
        cmd.Parameters.AddWithValue("@MISaction", MISaction);
        cmd.Parameters.AddWithValue("@BogeyType", BogeyType);

更新1

cmd.CommandText =
        "INSERT into tblMissEvents (EventTime,MIS_Event_Action, Bogey_Type) 
                                Values 
                                   (@date, @MISaction, @BogeyType)"; 

更新2

int RowUpdated=cmd.ExecuteNonQuery();
MessageBox.Show("Number of row updated"+RowUpdated);