问题
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());
}
回答1:
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=Trueand everything else is exactly the same as before...
回答2:
What you say simply doesn't happen. Either the query will insert a record, or you will get an exception.
There is an error in your query, though. There shouldn't be apostrophes around the @CustAdd parameter, that will make the database interpret that as a literal string, not a parameter:
string query = "insert into Customer(CustomerName,CustomerAddress,PhoneNo) values (@CustNm,@CustAdd,@Ph)";
I'm not certain how the database would react, but you would either get an exception telling you that you have a parameter that is never used in the query, or it would insert a record with the value @CustAdd as the address.
回答3:
- Are you sure you're looking at the right database to verify that no data is being inserted?
- Are you performing this insert within the context of a transaction that isn't being committed?
回答4:
You didnt specify the same paramets, (forgot to add "@" do it like:
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);
回答5:
Is CustomerId setup as an AutoIncrementing PK on the Customer table? Also, you do not need the single quote (') on the '@CustAdd' param;
来源:https://stackoverflow.com/questions/9382756/data-is-not-inserting-into-table