2

私はこれSqlDataSourceを私の形で持っています:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 

        InsertCommand="INSERT INTO [contact] ([Name], [phone], [email], [comment], [time]) VALUES (@Name, @phone, @email, @comment, @time)" 
        oninserting="SqlDataSource1_Inserting" >
        <InsertParameters>
            <asp:ControlParameter ControlID="TBname" Name="Name" PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="TBphone" Name="phone" PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="TBemail" Name="email" PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="TBmessage" Name="comment" PropertyName="Text" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>

そして、最初の 4 つのフィールドに TextBoxes の値を保存し、5 番目のフィールドに現在の時刻を保存する必要があるこの関数があります。

protected void SaveToDB(object sender, EventArgs e)
{
        SqlDataSource1.InsertParameters["time"].DefaultValue = DateTime.Now.ToString();
        SqlDataSource1.Insert();
}

5 つのフィールドのうち 4 つが、フォームの一部の TextBoxes から値を取得します。問題は最後のフィールドtimeです。値を設定できません。関数が実行されると、次のエラーが表示されます。

例外の詳細: System.Data.SqlClient.SqlException: 文字列またはバイナリ データが切り捨てられます。ステートメントは終了されました。

私は何をすべきか?

時間を割いていただきありがとうございます。

4

2 に答える 2

3

コード ビハインドを使用せずに、SQL GetDate 関数を InsertCommand で直接使用できるはずです。

InsertCommand="INSERT INTO [contact] ([Name], [phone], [email], [comment], [time]) VALUES (@Name, @phone, @email, @comment, GetDate())"  
于 2012-09-25T21:31:11.977 に答える
2

これを試して:

DateTime.Now.ToShortDateString(); 

"文字列型やバイナリは省略されます。" 通常、データベース内のフィールドのサイズが、入れようとしているものよりも小さいことに関係しています。

于 2012-06-03T11:43:25.873 に答える