0

我在将应用程序中的数据插入数据库时​​遇到了一点问题。我在连接或尝试插入数据库时​​没有收到任何错误,但只是没有插入数据。我将 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();
    }

我检查了调试器,单击按钮时没有抛出任何错误,但是当我检查数据库中的表时,它仍然是空的。

4

2 回答 2

0

您需要使用 ExecuteNonQuery 执行查询。检查以下代码。

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));
    cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception Ex)
                {
                    Console.WriteLine("Unable To Save Data. Error - " + Ex.Message);
                }
            }
        }
于 2013-06-16T13:18:02.867 回答
0

您需要执行非查询,因为您没有重新访问您仅插入的任何数据,并且您还需要 finally 块以确保即使出现异常连接也会关闭

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));
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception Ex)
                {
                    Console.WriteLine("Unable To Save Data. Error - " + Ex.Message);
                }
                finally
                {
                    con.Close();
                }
            }
        }
于 2013-06-16T13:25:01.903 回答