c# - SQL C# insert 命令不起作用

标签 c# mysql sql database sql-insert

我在将数据插入数据库时​​遇到问题。 我可以使用选择查询从数据库中读取数据,所以我知道我的连接字符串是正确的,但由于某种原因插入不起作用。 这是我的代码:

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

它没有显示任何异常,当我通过 Visual studio 资源管理器检查我的表时 表中未添加任何内容。 我很难弄清楚这一点,所以 我很感激任何提供帮助的人

最佳答案

我相信您忘记了连接中的初始目录。因为如果没有它,您的代码将尝试在 master (默认数据库)中运行。 这是一个应该对您有帮助的代码片段,为了您的利益,您显然需要修改代码。

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;

    }
}

}

抱歉,我无法为您提供更多信息,但这是我注意到缺少的东西,并且基于我无法提供的无错误。

这里是进一步阅读: http://technet.microsoft.com/en-us/library/aa905872(v=sql.80).aspx

关于c# - SQL C# insert 命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18385110/

相关文章:

mysql - 如何返回表中与 MySQL 中另一列共享相同值的重复项?

C# : Serialize objects to XML without reflection

c# - 停止 WPF 动画, Storyboard 在 xaml 中开始但在代码隐藏中停止?

直接工作的查询的 mysql 语法错误

sql - 如何只更新一定比例的匹配值

sql - 如何将持续时间添加到 hhmm 格式的字符串并将其转换回字符串?

c# - INNER 和 OUTER Join 的 LINQ 方法语法

c# - 使用 C# 在 Web API 中使用调度程序

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link failure

mysql - SQL中如何获取距离最短的3条记录?