简体   繁体   中英

SQL c# insert Command doesn't work

I'm having a problem insertin data into my database. I can read from the database by using the select query, so I know my connection string is correct, but for some reason the insert doesn't work. Here's my code:

private string ConnectionString()
{
    return @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\dbBusiness.mdf;Integrated Security=True;User Instance=True";
}
private void Insert()
{
   try{
        string sqlStrInsert = "INSERT INTO myTable ([param1],[param2])VALUES(@param1,@param2)";
        SqlConnection connection = new SqlConnection(ConnectionString());
        SqlCommand command = new SqlCommand(sqlStrInsert, connection);
        command.Parameters.Add("@param1", SqlDbType.SmallInt);
        command.Parameters.Add("@param2", SqlDbType.NVarChar,50);
        command.Parameters["@param1"].Value = numOf_company;
        command.Parameters["@param2"].Value = txt_name.Text;
        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
      }
   catch(Exception ex)
      {
        throw new Exception(ex.ToString(), ex);
      }
}

It doesn't show any exeption, and when I check my table through the Visual studio explorer nothing is added to the table. Im having trouble figuring this out so i'd appreciate anyone who helps

Who is TABLE!?!? The name of your table? If yes, please change this name because if a reserved keyword

INSERT INTO table (param1,param2)VALUES(@param1,@param2)

becomes (for example)

INSERT INTO myTable (param1,param2)VALUES(@param1,@param2)

I believe you forgot your Initial Catalog in your connection. Because without it your code will try to run in master (the default database). Here is a snippet that should help you, you will need to modify the code obviously for your benefit.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Working
{
class Program
{
    static void Main(string[] args)
    {
        string DsQuery = "INSERT INTO table (param1,param2)VALUES(@param1,@param2)"; 
        string TgtServer = @".\SQLEXPRESS";
        DataSet dsServers = new DataSet();
        dsServers = ExecQuery(DsQuery, TgtServer, "InitialCatalog");
    }

    private static DataSet ExecQuery(string strQuery, string strServer, string strIC)
    {

        string connectionString = @"Data Source=" + strServer + ";Initial Catalog=" + strIC + ";Integrated Security=SSPI";
        string commandString = strQuery;

        DataSet ds = new DataSet();
        try
        {
            SqlDataAdapter da = new SqlDataAdapter(commandString, connectionString);
            da.Fill(ds, "Table");
        }
        catch (SqlException e)
        {
            Console.WriteLine("An SQL Exception Occured: " + e.Message);

        }
        catch (Exception e)
        {
            Console.WriteLine("An General Exception Occured: " + e.Message);

        }

        return ds;

    }
}

}

Sorry I can't give you anything more but that is something I noticed was missing and based on the no error given I can't provide.

Here is further reading: http://technet.microsoft.com/en-us/library/aa905872(v=sql.80).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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