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.

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.

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

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归因
不隶属于 StackOverflow
scroll top