数据不会被插入到表? [英] Data is not inserting into table?
问题描述
我的表名是客户
。它有四列
客户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快速的密度纤维板
文件 - 我几乎可以肯定你的数据是否有
在真正的解决方案在我看来,将
-
安装SQL Server Express(和你已经做到这一点无论如何)
-
安装SQL Server Management Studio中快速
-
在 SSMS快递,给它一个合理的名称(如
VictoryDatabase
) -
连接使用其逻辑的数据库名称为(当你在服务器上创建它交给) - 和周围不乱用物理数据库文件和用户实例。在这种情况下,您的连接字符串会是这样:
数据源= .\\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
install SQL Server Express (and you've already done that anyway)
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g.
VictoryDatabase
)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屋!