且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Customer,Company和Cust_Comp这三个表中插入数据,这三个表是使用存储过程的前两个表的链接表

更新时间:2023-01-31 12:58:06

你确定它有效吗? ?



dbCmd.Parameters.AddWithValue没有采用db类型的重载。您将值设置为SqlDbType.NVarChar然后更改它。



请尝试使用此语法。它更具可读性,恕我直言:



Are you sure it works at all?

dbCmd.Parameters.AddWithValue has no overloads that take a db type. You are setting the value as SqlDbType.NVarChar then changing it.

Try this syntax instead. It's a little more readable, IMHO:

dbCmd.Parameters.AddRange(new[]
{
    new SqlParameter("@FirstName", SqlDbType.NVarChar) {Value = FirstName},
    new SqlParameter("@LastName", SqlDbType.NVarChar) {Value = LastName},
    new SqlParameter("@CellNo", SqlDbType.NVarChar) {Value = CellNo},
    new SqlParameter("@EmailAddress", SqlDbType.NVarChar) {Value = EmailAddress},
    new SqlParameter("@CompanyName", SqlDbType.NVarChar) {Value = CompanyName},
    new SqlParameter("@BuildingName", SqlDbType.NVarChar) {Value = BuildingName},
    new SqlParameter("@CompanyCity", SqlDbType.NVarChar) {Value = CompanyCity},
    new SqlParameter("@CompanyStreet", SqlDbType.NVarChar) {Value = CompanyStreet},
    new SqlParameter("@CompanyTown", SqlDbType.NVarChar) {Value = CompanyTown},
    new SqlParameter("@CompanyPostalCode", SqlDbType.NVarChar) {Value = CompanyPostalCode},
    new SqlParameter("@CompanyTelephoneNo", SqlDbType.NVarChar) {Value = CompanyTelephoneNo},
    new SqlParameter("@Country", SqlDbType.NVarChar) {Value = Country},
    new SqlParameter("@CompanyWebsite", SqlDbType.NVarChar) {Value = CompanyWebsite},
    new SqlParameter("@RegistrationNo", SqlDbType.NVarChar) {Value = RegistrationNo},
    new SqlParameter("@Town", SqlDbType.NVarChar) {Value = Town},
    new SqlParameter("@City", SqlDbType.NVarChar) {Value = City},
    new SqlParameter("@Streeet", SqlDbType.NVarChar) {Value = Street},
    new SqlParameter("@TelephoneNo", SqlDbType.NVarChar) {Value = TelephoneNo},
    new SqlParameter("@DateOfBirth", SqlDbType.Date) {Value = DateOfBirth},
    new SqlParameter("@CompanyLogo", SqlDbType.VarBinary) {Value = CompanyLogo}
});





这样我们正确设置类型并在初始化ctor中构建对象。



我不知道这是否会解决问题他发出你的问题,但它会修复你向我们展示的代码^ _ ^



如果能解决问题,请告诉我。

安迪





更新:OP实施了变化,但仍然没有快乐(我以为可能没有)



现在我们继续调试。你C#不会返回任何异常(或者你会告诉我们)所以bug必须在db中发生。这可能是我们没有看到的SP(存储过程)的问题,或者服务器上可能存在触发器或其他灾难性机制。我们可以向存储过程添加错误和错误检查:



首先,将整个事物包装在一个事务中。这样,如果有一件事失败,我们可以回滚所有内容:



This way we set the type correctly and build the object in the initialize ctor.

I don't know if that will fix the issue you have, but it will fix the code you have shown us ^_^

Let me know if that solves it.
Andy


UPDATE: OP has implemented the changes but still no joy (I thought there might not be)

Now we move on to debugging. You C# doesn't return any exception (or you would have told us) so the bugs must be happening within the db. This could be an issue with the SP (Stored Procedure) that we just aren't seeing, or there might be a trigger or some other disastrous mechanism on the server. We can add error and bug checking to the stored procedure:

First off, wrap the whole thing in a transaction. That way, if one thing fails we can roll back everything:

ALTER PROCEDURE [dbo].[AddCustomer]
@CompanyName nvarchar(50),
--blah blah
as 
Begin Transaction
--Do stuff
Commit Transaction





所有这一切都是启动一个新事务并提交它。如果发生错误,我们需要一种回滚方式:





All this does is start a new transaction and commits it. We need a way to rollback if an error occurs:

ALTER PROCEDURE [dbo].[AddCustomer]
@CompanyName nvarchar(50),
--blah blah
as 
BEGIN
  Declare @ErrorNum INT
  Begin Transaction
  --Do stuff
  SET @ErrorNum = @@ERROR
  IF @ErrorNum <> 0
  BEGIN
    Rollback Transaction
    return @@ErrorNum
  END
  --Do the next bit of stuff

  SET @ErrorNum = @@ERROR
  IF @ErrorNum <> 0
  BEGIN
    Rollback Transaction
    return @@ErrorNum
  END
  
  Commit Transaction
 
  return 0
END





现在我们可以判断是否有任何实际错误发生了,我们可以通过意外问题使数据完好无损。



让我们更深入



我们没有处理错误消息的流程,我们可以使用它来生成我们自己的^ _ ^





So now we can tell if any actual errors occur, and we can leave the data undamaged by unexpected issues.

"Lets go deeper"

No we have the process in place to handle error messages, we can use this to generate our own ^_^

ALTER PROCEDURE [dbo].[AddCustomer]
@CompanyName nvarchar(50),
--blah blah
as 
BEGIN
  Declare @ErrorNum INT --There are a set number of Error Numbers.  Each have a message associated.  There are many numbers that are left for use to use

  --Check Parameters are all ok first
  IF(badParams)
  BEGIN
     RAISERROR(50001, "Bad Parameter",10)
     --We use RAISERROR (only 1 'E'"). Custom errornum starts at 50k. Message can be used as return too. Last digit is severity: 10=don't halt, 16=halt.  We want don't halt because we want to make sure we close off the transaction
  END
  SET @ErrorNum = @@ERROR
  IF @ErrorNum <> 0
  BEGIN
    Rollback Transaction
    return @@ErrorNum
  END

  --Check record inserted
  IF(FailedInsert)
  BEGIN
     RAISERROR(50002, "Failed Insert 1",10)
  End

  Begin Transaction
  --Do stuff
  SET @ErrorNum = @@ERROR
  IF @ErrorNum <> 0
  BEGIN
    Rollback Transaction
    return @@ErrorNum
  END
  --Do the next bit of stuff

  --Check record inserted
  IF(FailedInsert)
  BEGIN
     RAISERROR(50003, "Failed Insert 2",10)
  End

  SET @ErrorNum = @@ERROR
  IF @ErrorNum <> 0
  BEGIN
    Rollback Transaction
    return @@ErrorNum
  END
  
  Commit Transaction
 
  return 0
END







你可以使用C#代码中的返回值。 0 =一切都好,50001 =坏参数,50002& 50003 =数据没有插入。







到目前为止它什么也解决不了,但它会告诉我you where the error is :)



I know it seems long winded, but this is how composite SPs should be written



There is much online regarding Error Handling and RAISERROR online if you want to know more.



Let me know how it goes ^_^



Andy




You can the use the return value in your C# code. 0 = all ok, 50001 = bad Params, 50002 & 50003 = data didn't insert.



So far it solves nothing, but it will tell you where the error is :)

I know it seems long winded, but this is how composite SPs should be written

There is much online regarding Error Handling and RAISERROR online if you want to know more.

Let me know how it goes ^_^

Andy


Try this it will work for you...

also in procedure give the data time properly.

and also see that \"dateofbirth\" in
Try this it will work for you...
also in procedure give the data time properly.
and also see that "dateofbirth" in
Quote:

dbCmd.Parameters.AddWithValue(\"@DateOfBith\" , DateOfBirth);

dbCmd.Parameters.AddWithValue("@DateOfBith", DateOfBirth);



is in date format.


is in date format.

dbCmd.Parameters.AddWithValue("@FirstName", FirstName);
dbCmd.Parameters.AddWithValue("@LastName", LastName);
dbCmd.Parameters.AddWithValue("@CellNo", CellNo);
dbCmd.Parameters.AddWithValue("@EmailAddress", EmailAddress);
dbCmd.Parameters.AddWithValue("@CompanyName", CompanyName);
dbCmd.Parameters.AddWithValue("@BuildingName", BuildingName);
dbCmd.Parameters.AddWithValue("@CompanyCity", CompanyCity);
dbCmd.Parameters.AddWithValue("@CompanyStreet", CompanyStreet);
dbCmd.Parameters.AddWithValue("@CompanyTown", CompanyTown);
dbCmd.Parameters.AddWithValue("@CompanyPostalCode", CompanyPostalCode);
dbCmd.Parameters.AddWithValue("@CompanyTelephoneNo", CompanyTelephoneNo);
dbCmd.Parameters.AddWithValue("@Country", Country);
dbCmd.Parameters.AddWithValue("@CompanyWebsite", CompanyWebsite);
dbCmd.Parameters.AddWithValue("@RegistrationNo", RegistrationNo);
dbCmd.Parameters.AddWithValue("@Town", Town);
dbCmd.Parameters.AddWithValue("@City",City);
dbCmd.Parameters.AddWithValue("@Streeet", Street);
dbCmd.Parameters.AddWithValue("@TelephoneNo",TelephoneNo);
dbCmd.Parameters.AddWithValue("@DateOfBith", DateOfBirth);

dbCmd.Parameters.AddWithValue("@CompanyLogo", CompanyLogo);
</pre>