简体   繁体   中英

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

I've created a local DB and a WCF Service that is supposed to insert things in a table called Vehicles, but i keep getting a Exception. The service is consumed by a website. The code for my .svc file is:

string Message;
using (SqlConnection con = new SqlConnection("Server=localhost;Database=Vehicles;"))
{
    con.Open();
    using (var cmd = new SqlCommand("INSERT INTO Vehicles(make,model,reg_nr,build_date,colour) values(@make,@model,@reg_nr,@build_date,@colour)", con))
    {
        cmd.Parameters.AddWithValue("@make", carInfo.Make);
        cmd.Parameters.AddWithValue("@model", carInfo.Model);
        cmd.Parameters.AddWithValue("@reg_nr", carInfo.Reg_nr);
        cmd.Parameters.AddWithValue("@build_date", carInfo.Build_date);
        cmd.Parameters.AddWithValue("@colour", carInfo.Colour);
        int result = cmd.ExecuteNonQuery();
        if (result == 1)
        {
            Message = carInfo.Make + " " + carInfo.Model + " => Success!";
        }
        else
        {
            Message = "Oops!";
        }
        con.Close();
        return Message;
    }
}

I get the exception listed in the title at con.Open(); . The build_date it's also nvarchar(50), because i want to make the insert work first.

Why is it giving me this exception?

确保确切的数据库连接和Columns,Datatypes。

I have this code and this is working fine!

using (connection = new SqlConnection(Auth2Manager.getAuthConnectionString()))
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO " + Tables.TABLE_USERS + " (" + Columns.USER_NAME + ","
                    + Columns.PASSWORD + "," + Columns.TOKEN + ","
                    + Columns.CREATED_DATE + "," + Columns.UPDATED_DATE + "," + Columns.UPDATED_BY
                    + "," + Columns.IS_ACTIVE + "," + Columns.APP_ID + ")" +
                " VALUES (@UserName, @Password, @Token, @CreatedDate, @UpdatedDate, @UpdatedBy, @IP, @IsActive, @AppId)");
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection = connection;

                string token = Helper.generateUniqueToken();
                tokenGenerated = token; 
                Console.WriteLine(token);

                cmd.Parameters.AddWithValue("@UserName", user.UserName);
                cmd.Parameters.AddWithValue("@Password", user.Password);
                cmd.Parameters.AddWithValue("@Token", token);

                dateNow = DateTime.Now;
                date = dateNow.Year.ToString() + "-" + dateNow.Month.ToString() + "-" + dateNow.Day.ToString();

                cmd.Parameters.AddWithValue("@CreatedDate", date);
                cmd.Parameters.AddWithValue("@UpdatedDate", date);
                cmd.Parameters.AddWithValue("@UpdatedBy", user.UserName);
                cmd.Parameters.AddWithValue("@IsActive", 1);
                cmd.Parameters.AddWithValue("@AppId", user.ApplicationID);

                connection.Open();
                resultCount = cmd.ExecuteNonQuery();
            }
            if (resultCount > 0)
            {
                return true;
            }
            else
                return false;
        }
        else {
            tokenGenerated = null;
            return false;
        }

Try open connection after setting all parameters for SqlCommand . Moreover! I have Connection String in this form Data Source=PP-DEV-DMS-01;Initial Catalog=SalesDWH2;Integrated Security=SSPI . I hope this will help!

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM