简体   繁体   English

将VS2010与SqlClient和SQL Server 2012一起使用

[英]Using VS2010 with SqlClient and SQL Server 2012

I'm having a little problem inserting data from my application into my database. 将应用程序中的数据插入数据库时​​,我遇到了一些问题。 I do not get any errors while connecting or trying to insert into the database but data just doesn't get inserted. 在连接或尝试插入数据库时​​,我没有收到任何错误,但只是没有插入数据。 I use Visual Studio 2010 with C# and SqlClient and SQL Server 2012 Management Studio. 我将Visual Studio 2010与C#和SqlClient以及SQL Server 2012 Management Studio一起使用。

Here is my code: 这是我的代码:

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);
            }
        }
    }
}

And here is the button click event: 这是按钮单击事件:

protected void btnOk_Click(object sender, ImageClickEventArgs e)
    {
        Sources src = new Sources();
        src.mSourceID = 1;
        src.mSourceName = "aboa";
        src.mActive = 0;
        src.insertData();
    }

I've checked the debugger and do not get any errors thrown when I click the button, but when I check my table in my database it remains empty. 我已经检查了调试器,并且单击按钮时没有抛出任何错误,但是当我检查数据库中的表时,它仍然为空。

You need to execute your query using ExecuteNonQuery .check the below code. 您需要使用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);
                }
            }
        }

You need to executenonquery because you are not retourning any data you are only inserting and u also need the finally block to ensure that even if there is an exception the connection will be closed 您需要执行nonquery,因为您不会重新浏览仅插入的任何数据,并且您还需要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();
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM