2013-08-22 85 views
0

我有问题插入数据到我的数据库中。 我可以通过使用select查询从数据库读取,所以我知道我的连接字符串是正确的,但出于某种原因插入不起作用。 这里是我的代码:SQL c#插入命令不起作用

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

它不显示任何exeption,当我通过Visual Studio的探险家检查我的表 没有被添加到表中。 即时通讯解决这个问题,所以 我会很感谢任何人谁帮助

+2

请告诉我们它是如何不起作用。简单地说,“它不起作用”并不是**在任何可以接近的地方**足够的信息。它错误吗?如果是这样,用什么?在哪一行? – Arran

+0

你收到了什么错误信息? – Tommassiov

+0

使用try/catch和debug查看你得到了什么异常 – Zaki

回答

0

相信您忘记您的初始目录中的连接。因为没有它,你的代码将尝试在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

+0

在初始目录中需要写什么?它是:| DataDirectory | \ App_Data \ dbBusiness.mdf还是别的吗? – user2139184

+0

要连接到的数据库的名称。 –

+0

我使用了上面写的但它仍然不起作用 – user2139184

0

谁是TABLE!?!?你的桌子的名字?如果是,请因为如果保留关键字

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

成为(例如)改变这个名字

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

不需要改变,只是在它周围包裹一个'[]'。 –

+0

我当然希望他只是改了问题的名字 – Jonesopolis

+0

@王金:是的,但最好不要使用这个关键字 –