将DatetimePicker的Date值发送到SQL Server的正确格式

时间:2017-01-16 20:15:38

标签: c# sql-server winforms

SQL Server在"崩溃"中包含以下列:表

   (<NationalId, char(10),>
   ,<CrashTime, date,>
   ,<CrashLocation, nvarchar(50),>
   ,<City, nvarchar(50),>
   ,<CarColor, nvarchar(50),>
   ,<CarEngine, nvarchar(50),>
   ,<CarType, nvarchar(50),>
   ,<Damage, int,>)

有一个存储过程被调用&#34; addCrash&#34;使用以下代码:

ALTER Procedure [dbo].[addCrash]

           @NationalId char(10),
           @CrashTime date,
           @CrashLocation nvarchar(50),
           @City nvarchar(50),
           @CarColor nvarchar(50),
           @CarEngine nvarchar(50),
           @CarType nvarchar(50),
           @Damage int
AS
BEGIN          



INSERT INTO [dbo].[Crash]
           ([NationalId]
           ,[CrashTime],
           [CrashLocation]
           ,[City]
           ,[CarColor]
           ,[CarEngine]
           ,[CarType]
           ,[Damage])
     VALUES


(         @NationalId,
          @CrashTime ,
           @CrashLocation ,
           @City ,
           @CarColor ,
           @CarEngine ,
           @CarType ,
           @Damage)
END

现在在Visual Studio中,我使用自定义格式为yyyy-MM-dd的DateTimePicker将日期发送到&#34; CrashTime&#34;列,问题是每当我尝试将日期从DateTimePicker添加到我的数据库时,有错误的是没有发送&#34; CrashTime&#34;(日期)正确。

private void button1_Click(object sender, EventArgs e)
    {

    SqlParams[] parameters = new SqlParams[]
    {
        new SqlParams("@NationalId", SqlDbType.Char, nationalId),
        new SqlParams("@CrashTime", SqlDbType.Date,  dateTimePicker1.Value.Date.ToString("yyyy-MM-dd")),
        new SqlParams("@CrashLocation", SqlDbType.NVarChar, TxtCrashLocation.Text),
        new SqlParams("@City", SqlDbType.NVarChar, cityCombo.SelectedValue.ToString()),
        new SqlParams("@CarColor", SqlDbType.NVarChar, ColorCombo.SelectedValue.ToString()),
        new SqlParams("@CarEngine", SqlDbType.NVarChar, EngineCombo.SelectedValue.ToString()),
        new SqlParams("@CarType", SqlDbType.NVarChar, cartypeCombo.SelectedValue.ToString()),
        new SqlParams("@Damage", SqlDbType.Int, TxtDamage.Text)

    };

    ModifyConnection mc = new ModifyConnection();
    mc.AddRecord("exec addCrash @NationalId,@CrashTime,@CrashLocation,@City,@CarColor,@CarEngine,@CarType,@Damage", parameters);
    MessageBox.Show("Crash added Successfuly");
} 

我不知道问题的根源,即使我将DateTimePicker的值设置为字符串格式听起来正确并且是:2017-01-05

1 个答案:

答案 0 :(得分:3)

您必须为参数指定正确的数据类型。 Date和DateTime有效,Date和String not。

纠正这个:

post_tag

试试这个:

<?php

2016_12_18_230831_create_post_tag_table.php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostTagTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('post_tag', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('post_id')->unsigned();
        $table->foreign('post_id')->references('id')->on('posts');
        $table->integer('tag_id')->unsigned();
        $table->foreign('tag_id')->unsigned();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('post_tag');
}
}

SQL Server中使用的数据类型“Date”接受Visual Studio项目中的“DateTime”变量。