将数据插入数据库表时出错

时间:2022-05-19 17:03:52

When I insert data into the database table, only the first letter of the data is inserted in the columns of the database.

当我将数据插入数据库表时,只有数据的第一个字母插入数据库的列中。

Here are the database tables:

以下是数据库表:

将数据插入数据库表时出错

For the employee id:102, only the first letter of the data is inserted. This is the Database Table Schema used.

对于员工ID:102,仅插入数据的第一个字母。这是使用的数据库表模式。

I'm using C# in Visual Studio Community 2015 and SQL Server Studio Management Studio for my project.

我在Visual Studio Community 2015中使用C#,在我的项目中使用SQL Server Studio Management Studio。

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimeSheetApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string connection = ConfigurationManager.ConnectionStrings["TimeSheetAppDbConnectionString"].ToString();
            var dbcontext = new TimeSheetAppDesignDataContext(connection);

            int EmployeeID = 102;
            string EmployeeName = "Riyas", EmployeeEmailAddress = "riyas@gmail.com", EmployeeLocation = "SriLanka";
            dbcontext.insertEmployee(EmployeeID, EmployeeName, EmployeeEmailAddress, EmployeeLocation);

            Console.WriteLine("Data inserted successfully");
            Console.ReadLine();
        }
    }
}

Anyone kindly help me to resolve this error.

任何人都帮我解决这个错误。

1 个解决方案

#1


2  

You're passing the data correctly, and your database structure is correct. The last clue to find the error is to check the procedure insertEmployee, the error is in the procedure and most probably in the parameters type.

您正确传递数据,并且数据库结构正确。找到错误的最后一条线索是检查过程insertEmployee,错误在过程中,最有可能在参数类型中。

Check the parameters definition for that procedure. If you're using NVARCHAR without specifying the length, then the data is being truncated to a single character because the default for NVARCHAR is NVARCHAR(1)

检查该过程的参数定义。如果您在未指定长度的情况下使用NVARCHAR,则数据将被截断为单个字符,因为NVARCHAR的默认值为NVARCHAR(1)

#1


2  

You're passing the data correctly, and your database structure is correct. The last clue to find the error is to check the procedure insertEmployee, the error is in the procedure and most probably in the parameters type.

您正确传递数据,并且数据库结构正确。找到错误的最后一条线索是检查过程insertEmployee,错误在过程中,最有可能在参数类型中。

Check the parameters definition for that procedure. If you're using NVARCHAR without specifying the length, then the data is being truncated to a single character because the default for NVARCHAR is NVARCHAR(1)

检查该过程的参数定义。如果您在未指定长度的情况下使用NVARCHAR,则数据将被截断为单个字符,因为NVARCHAR的默认值为NVARCHAR(1)