通过C#和Asp.net将DateTime插入Access数据库

通过C#和Asp.net将DateTime插入Access数据库,c#,asp.net,datetime,C#,Asp.net,Datetime,这可能是一个非常常见的老问题,但我仍在努力解决很多问题,最后我将其发布在这里: 我必须在Access数据库中插入DateTime,但这样做有困难。 我的C#代码: 以及相应的Asp.Net脚本: <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/events.accdb" SelectCommand="SELECT * FROM [upcoming]" Ins

这可能是一个非常常见的老问题,但我仍在努力解决很多问题,最后我将其发布在这里:

我必须在Access数据库中插入DateTime,但这样做有困难。 我的C#代码:

以及相应的Asp.Net脚本:

 <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
        DataFile="~/events.accdb" SelectCommand="SELECT * FROM [upcoming]"

        InsertCommand="INSERT INTO [upcoming] ([eventname], [description],[coordinator], [venue],[time])
         VALUES (@eventname1, @description1,@coordinatior1, @venue1, @time1)" >


        <InsertParameters>
          <asp:Parameter Name="eventname1" Type="String" />
          <asp:Parameter Name="description1" Type="String" />
          <asp:Parameter Name="coordinator1" Type="String" />
          <asp:Parameter Name="venue1" Type="String" />
          <asp:Parameter Name="time1" Type="DateTime" />

        </InsertParameters>

    </asp:AccessDataSource>
<asp:Panel ID="Panel2" runat="server">
   <table>
            <tr><td><asp:Label ID="Label6" runat="server" Text="Name:"></asp:Label></td>
           <td class="style1"> <asp:TextBox ID="TextBox6" runat="server" 
                    Height="24px" Width="548px"></asp:TextBox></td></tr>

            <tr><td><asp:Label ID="Label7" runat="server" Text="Description:"></asp:Label></td>
           <td class="style1"> <asp:TextBox ID="TextBox7" runat="server" 
                    Height="155px" TextMode="MultiLine" 
                   Width="555px"></asp:TextBox></td></tr>

            <tr><td><asp:Label ID="Label8" runat="server" Text="Event Coordinators:"></asp:Label></td>
           <td class="style1"> 
               <asp:TextBox ID="TextBox8" runat="server" 
                 Width="548px"></asp:TextBox></td></tr>

           <tr><td> <asp:Label ID="Label9" runat="server" Text="Venue:"></asp:Label></td>
            <td class="style1">
                <asp:TextBox ID="TextBox9" runat="server"  
                 Width="546px"></asp:TextBox></td></tr>

            <tr><td><asp:Label ID="Label10" runat="server" Text="Date &Time:"></asp:Label></td>
            <td class="style1">
                <asp:TextBox ID="TextBox10" runat="server" 
                 Height="23px" Width="543px"></asp:TextBox></td>
                </tr>

           <tr><td> 
               <asp:Button ID="Button5" runat="server" Text="Insert" 
                   onclick="Button5_Click" /></td>
           <td class="style1"> <asp:Button ID="Button6" runat="server" CommandName="Cancel" 
                   Text="Cancel" onclick="Button6_Click" /></td></tr>
            <table/>
</asp:Panel>


请帮助我通过给定的机制插入日期时间。

根据您的评论,您正在寻找将字符串转换为
datetime
类型的方法:

String time = TextBox10.Text;
DateTime dt = Convert.ToDateTime(time);

您将使用TryParse方法在运行时处理一个可能的异常

        string time = this.TextBox10.Text;
        DateTime dt = new DateTime();
        if (DateTime.TryParse("12/12/2011", out dt))
        {
            // your code here
        }

关于

“我这样做有困难。”-这不是一个真正的问题…@Waqas time1参数值在从c#代码文件传递到aspx文件时被转换为字符串,所以问题是,如何将其再次转换回datetime类型?你好,Anant Gupta,欢迎您
        string time = this.TextBox10.Text;
        DateTime dt = new DateTime();
        if (DateTime.TryParse("12/12/2011", out dt))
        {
            // your code here
        }