我在将应用程序中的数据插入数据库时遇到了一点问题。我在连接或尝试插入数据库时没有收到任何错误,但只是没有插入数据。我将 Visual Studio 2010 与 C#SqlClient
和 SQL Server 2012 Management Studio 一起使用。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
<summary>
Summary description for Sources
</summary>
public class Sources
{
private string conString;
public int mSourceID;
public string mSourceName;
public int mActive;
public Sources()
{
conString = WebConfigurationManager.ConnectionStrings["dbconstring"].ConnectionString;
mSourceID = 0;
mSourceName = "";
mActive = 0;
}
public void insertData()
{
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
try
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO dbo.Sources(SourceID, SourceName, Active) VALUES(@SourceID, @SourceName, @Active)", con))
{
cmd.Parameters.Add(new SqlParameter("SourceID", mSourceID));
cmd.Parameters.Add(new SqlParameter("SourceName", mSourceName));
cmd.Parameters.Add(new SqlParameter("Active", mActive));
}
}
catch (Exception Ex)
{
Console.WriteLine("Unable To Save Data. Error - " + Ex.Message);
}
}
}
}
这是按钮单击事件:
protected void btnOk_Click(object sender, ImageClickEventArgs e)
{
Sources src = new Sources();
src.mSourceID = 1;
src.mSourceName = "aboa";
src.mActive = 0;
src.insertData();
}
我检查了调试器,单击按钮时没有抛出任何错误,但是当我检查数据库中的表时,它仍然是空的。