本地数据库插入查询不执行任

时间:2014-12-02 17:01:26

标签: c# sql sql-server tsql ado.net

我正在尝试在数据库中插入一行。以下是我的询问:

using (SqlConnection conn = new SqlConnection("Data Source = (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Traindata.mdf;Integrated Security=True"))
{
    string query = "INSERT INTO dbo.Station (Naam, X, Y, Sporen) VALUES (@naam, @x, @y, @sporen)";

    using (SqlCommand command = new SqlCommand(query, conn))
    {
        command.Parameters.AddWithValue("@naam", insert[1]);
        command.Parameters.AddWithValue("@x", insert[2]);
        command.Parameters.AddWithValue("@y", insert[3]);
        command.Parameters.AddWithValue("@sporen", insert[4]);
        conn.Open();
        try
        {
            command.ExecuteNonQuery();
        }
        catch (SqlException exc)
        {
            Console.WriteLine("Error to save on database");
            Console.WriteLine(exc.Message);
        }
        conn.Close();
    }
}

当我运行它时没有任何反应(也没有SQL错误)。我究竟做错了什么?我很抱歉,如果这是一个愚蠢的问题,我只是一个初学者。

这应该可行(我已经使用了一个可行的选择查询对此进行了测试)。

2 个答案:

答案 0 :(得分:0)

您是否尝试将查询存储在存储过程中并从C#中调用它? ...这实际上比通过C#代码中的硬代码进行查询更容易...只需创建一个存储过程,执行您想要它做的任何事情,然后从C#调用它并添加参数。看起来应该是这样的:

                SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Your_Conection_String_s_Name"].ConnectionString);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "dbo.Your_Stored_Procedure";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Input_Param_1", SqlDbType.VarChar, 18).Value = "C#_Parameter1";
                cmd.Parameters.Add("@Input_Param_2", SqlDbType.VarChar, 45).Value = "C#Parameter_2";
                cmd.Parameters.Add("@Input_Param_3", SqlDbType.VarChar, 45).Value = "C#Parameter_3";
                cmd.Parameters.Add("@Input_Param_4", SqlDbType.Text).Value = "C#Parameter_4";
                cmd.Parameters.Add("@Input_Param_5", SqlDbType.VarChar, 45).Value = "C#Parameter_5";
                cmd.Parameters.Add("@Output_Parameter_1", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
                cmd.Parameters.Add("@Output_Parameter_2", SqlDbType.DateTime).Direction = ParameterDirection.Output;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                "C#Output_Parameter_1" = "" + cmd.Parameters["@Output_Parameter_1"].Value;
                "C#Output_Parameter_2" = "" + cmd.Parameters["@Output_Parameter_2"].Value;

希望它有所帮助。

答案 1 :(得分:0)

我的猜测是你的类型不匹配了 如果x,y不是int,则用正确的类型替换

using (SqlConnection conn = new SqlConnection("Data Source = (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Traindata.mdf;Integrated Security=True"))
{
    using (SqlCommand command = SqlCommand.CreateCommand())
    {
        try
        {
            conn.Open();

            command.Query = "select count(*) from dbo.Station";
            Int32 rowsRet = (Int32)command.ExecuteScalar();
            Console.WriteLine(rowsRet.ToString());

            command.Query = "INSERT INTO dbo.Station (Naam, X, Y, Sporen) VALUES (@naam, @x, @y, @sporen)";
            command.Parameters.AddWithValue("@naam", insert[1]);
            command.Parameters.Add("@x", SqlDbType.Int);
            command.Parameters["@x"].Value = Int32.Parse(insert[2]);
            command.Parameters.Add("@y", SqlDbType.Int);
            command.Parameters["@y"].Value = Int32.Parse(insert[3]);
            command.Parameters.AddWithValue("@sporen", insert[4]);       
            rowsRet = command.ExecuteNonQuery();
            Console.WriteLine(rowsRet.ToString());

            command.Query = "select count(*) from dbo.Station";
            Int32 rowsRet = (Int32)command.ExecuteScalar();
            Console.WriteLine(rowsRet.ToString());
        }
        catch (SqlException exc)
        {
            Console.WriteLine("Error to save on database");
            Console.WriteLine(exc.Message);
        }
        finally 
        {
            conn.Close();
        }

        // op claims the insert is gone the next time the programs is run 
        try
        {
            conn.Open();
            command.Query = "select count(*) from dbo.Station";
            Int32 rowsRet = (Int32)command.ExecuteScalar();
            Console.WriteLine(rowsRet.ToString());
        }
        catch (SqlException exc)
        {
            Console.WriteLine("Error to save on database");
            Console.WriteLine(exc.Message);
        }
        finally 
        {
            conn.Close();
        }
    }
}