c#.net Web应用程序..数据未保存在数据库中

时间:2018-08-01 02:58:11

标签: c# sql .net sql-server

我使用SQL Server Express 2014在Web应用程序上工作。在那里,我习惯于将一些数据设置到表中。没有错误发生,但是数据没有设置到表中。

    <?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.6"/>
    <httpRuntime targetFramework="4.6"/>
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
    </httpModules>
  </system.web>
  <connectionStrings>
    <add name="connect" connectionString="Data Source=LAKSHITHA\SQLEXPRESS;Initial Catalog=EMS1;Integrated Security =True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <remove name="ApplicationInsightsWebTracking"/>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
        preCondition="managedHandler"/>
    </modules>
  </system.webServer>
</configuration>

这是我的aspx.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication2
{
    public partial class Reg2 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connect"].ToString());


        protected void Page_Load(object sender, EventArgs e)
        {

            // con.ConnectionString = "Data Source=LAKSHITHA/SQLEXPRESS ;Initial Catalog=EMS1;Integrated Security=true";
            con.Open();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("insert into Epersonal " + " (Emp_Id,NIC,Gender,B_day,Nationality,Marital_status,Work_Role)values(@Emp_Id,@NIC,@Gender,@B_day,@Nationality,@Marital_status,@Work_Role)", con);
            cmd.Parameters.AddWithValue("@Emp_Id", TextBox1.Text);
            cmd.Parameters.AddWithValue("@NIC", nic.Text);
            cmd.Parameters.AddWithValue("@Gender", DropDownList2.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@B_day", bday.Text);
            cmd.Parameters.AddWithValue("@Nationality", ntionl.Text);
            cmd.Parameters.AddWithValue("@Marital_status", DropDownList3.SelectedItem.Value);
            cmd.Parameters.AddWithValue(" @Work_Role", DropDownList1.SelectedItem.Value);
            Response.Write("cannection made");
            Response.Redirect("log.aspx");
            // con.Open();
            con.Close();
            //cmd.ExecuteNonQuery();
        }
    }
}

[这是我的表格和tz属性] 设置数据后,数据库中没有数据。在我的日志记录表中,我已连接数据库,并在其中使用数据库数据并将日志记录到应用程序 你能在这里告诉我出什么问题了吗??

1 个答案:

答案 0 :(得分:1)

您没有执行命令。另外,将SqlCommand广告SqlConnection包装在using子句中。

using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connect"].ToString()) 
{
   using (SqlCommand cmd = new SqlCommand("insert into Epersonal (Emp_Id,NIC,Gender,B_day,Nationality,Marital_status,Work_Role)values(@Emp_Id,@NIC,@Gender,@B_day,@Nationality,@Marital_status,@Work_Role)", con))
   {
          cmd.Parameters.AddWithValue("@Emp_Id", TextBox1.Text);
          cmd.Parameters.AddWithValue("@NIC", nic.Text);
          cmd.Parameters.AddWithValue("@Gender", DropDownList2.SelectedItem.Value);
          cmd.Parameters.AddWithValue("@B_day", bday.Text);
          cmd.Parameters.AddWithValue("@Nationality", ntionl.Text);
          cmd.Parameters.AddWithValue("@Marital_status", DropDownList3.SelectedItem.Value);
          cmd.Parameters.AddWithValue(" @Work_Role", DropDownList1.SelectedItem.Value);
          con.Open();
          Response.Write("cannection made");
          cmd.ExecuteNonQuery();
  }
}
Response.Redirect("log.aspx");

编辑:

最好使用Add而不是AddWithValue,以避免在推断正确的数据库类型时出错。 例如:

cmd.Parameters.Add("@Nationality", SqlDbType.Varchar, 20).Value = ntion1.Text; //Picked a random size of 20, use your specified size instead

将其与所有值一起使用。