数据不会被插入到表? [英] Data is not inserting into table?

查看:134
本文介绍了数据不会被插入到表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表名是客户。它有四列

 客户ID 
客户名称
CustomerAddress
PHONENO

这是我的C#代码。我没有得到任何异常和数据不被插入到数据库中。

 字符串CONNSTRING =数据源= .\\ SQLEXPRESS; AttachDbFilename = | DataDirectory目录| \\VictoryDatabase.mdf;集成安全=真;用户实例=真; 
的SqlConnection MyConnection的=新的SqlConnection(CONNSTRING);


{
myConnection.Open();
字符串查询=插入到客户(客户名称,CustomerAddress,PHONENO)值(@CustNm,@ CustAdd',@博士);
的SqlCommand myCommand =新的SqlCommand(查询,MyConnection的);
myCommand.Parameters.AddWithValue(CustNm,打印[0] .CustomerName);
myCommand.Parameters.AddWithValue(CustAdd,打印[0]。地址);
myCommand.Parameters.AddWithValue(PH,打印[0] .Telephone);

Console.WriteLine(打印[0] .Telephone);

myCommand.ExecuteNonQuery();
myConnection.Close();
}
赶上(例外五)
{
Console.WriteLine(e.ToString());
}


解决方案

正如我以前说过在这个网站 - 整个的用户实例和AttachDbFileName = 的方法是有缺陷的 - 在最好的! Visual Studio将被复制周围的密度纤维板文件和最有可能的,您插入作品就好了 - 但你只是在看错密度纤维板最终文件



如果你想坚持使用这种方法,然后尝试把一个断点上的 myConnection.Close()通话 - 然后检查与SQL Server管理Studio快速的密度纤维板文件 - 我几乎可以肯定你的数据是否有



真正的解决方案在我看来,将




  1. 安装SQL Server Express(和你已经做到这一点无论如何)


  2. 安装SQL Server Management Studio中快速


  3. SSMS快递,给它一个合理的名称(如 VictoryDatabase


  4. 连接使用其逻辑的数据库名称为(当你在服务器上创建它交给) - 和周围不乱用物理数据库文件和用户实例。在这种情况下,您的连接字符串会是这样:

     数据源= .\\SQLEXPRESS;数据库= VictoryDatabase;集成安全性= TRUE 

    和其他一切都是究竟和以前一样。 ..



My table name is Customer. It has four columns

CustomerId
CustomerName
CustomerAddress
PhoneNo

This is my c# code. I am not getting any exceptions and data is not inserting into database.

string connString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\VictoryDatabase.mdf;Integrated Security=True;User Instance=True";
SqlConnection myConnection = new SqlConnection(connString);

try
{
   myConnection.Open();
   string query = "insert into Customer(CustomerName,CustomerAddress,PhoneNo) values (@CustNm,'@CustAdd',@Ph)";
   SqlCommand myCommand = new SqlCommand(query,myConnection);
   myCommand.Parameters.AddWithValue("CustNm",Print[0].CustomerName);
   myCommand.Parameters.AddWithValue("CustAdd",Print[0].Address);
   myCommand.Parameters.AddWithValue("Ph",Print[0].Telephone);

   Console.WriteLine(Print[0].Telephone);

   myCommand.ExecuteNonQuery();
   myConnection.Close();
}
catch (Exception e)
{
   Console.WriteLine(e.ToString());
}

解决方案

As I've said before on this site - the whole User Instance and AttachDbFileName= approach is flawed - at best! Visual Studio will be copying around the .mdf file and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. VictoryDatabase)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=VictoryDatabase;Integrated Security=True
    

    and everything else is exactly the same as before...

这篇关于数据不会被插入到表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆