[英]Inserting data into SQL Server by using a stored procedure
I have a problem while inserting records into SQL Server. 将记录插入SQL Server时出现问题。
The string from C# doesn't show up in SQL Server as I'm inserting the sql just insert the first char 当我插入sql时,仅插入第一个字符,C#的字符串就不会出现在SQL Server中
Example: If I insert 22222 in the data base just the first 2 inserted 示例:如果我在数据库中插入22222,则仅插入前2个
Note I'm using a stored procedure for my first time. 注意我是第一次使用存储过程。
This is my code: 这是我的代码:
public void insert_workshop(DateTime Pdate, string PTime, string PDesc, byte[] Img)
{
SqlCommand cmd = new SqlCommand("[InsertWorkShops]", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@WorkshopsDate", SqlDbType.Date).Value = Pdate;
cmd.Parameters.Add("@Time", SqlDbType.NVarChar).Value = PTime;
cmd.Parameters.Add("@WorkshopsDescription", SqlDbType.NVarChar).Value = PDesc;
cmd.Parameters.Add("@WorkshopsImage", SqlDbType.Image).Value = Img;
cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value = 1;
try
{
cmd.ExecuteNonQuery();
Msg = "Add Done ";
}
catch
{
Msg = "Error While Adding";
}
WorkShopTransactions Ws = new WorkShopTransactions();
Ws.insert_workshop(WorkShopDT.Value, txtWorshopTime.Text.ToString(),
txtWorkshopDescriptions.Text.ToString(), img);
T-SQL: T-SQL:
ALTER PROCEDURE [dbo].[InsertWorkShops]
@WorkshopsDate date,
@Time nvarchar,
@WorkshopsDescription nvarchar,
@WorkshopsImage image,
@CreatedBy int
AS
BEGIN
SET NOCOUNT ON;
insert into Workshops
values(@WorkshopsDate, @Time, @WorkshopsDescription, @WorkshopsImage, @CreatedBy)
END
In the stored procedure, you have declared your nvarchar
parameters without a length. 在存储过程中,您声明的
nvarchar
参数没有长度。 They default to nvarchar(1)
. 它们默认为
nvarchar(1)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.