0

我得到 sqlException 未处理。'nvarchar' 附近的语法不正确和 'Name' 附近的语法不正确。我使用 SMO 创建了一个这样的表

Table TechOpsProjectTracker = new Table(db, "TechOpsProjectTracker");

//Add column ID
DataType dt = new DataType(SqlDataType.Int);
Column idColumn = new Column(TechOpsProjectTracker, "ID", dt);
TechOpsProjectTracker.Columns.Add(idColumn);

//Add Column Alignment
DataType dt1 = new DataType(SqlDataType.NVarChar, 50);
Column Column1 = new Column(TechOpsProjectTracker, "Alignment", dt1);
TechOpsProjectTracker.Columns.Add(Column1);

//Add Column Project Name
DataType dt2 = new DataType(SqlDataType.NVarChar, 50);
Column Column2 = new Column(TechOpsProjectTracker, "Project Name", dt2);
TechOpsProjectTracker.Columns.Add(Column2);

//Add Column Project Description
DataType dt3 = new DataType(SqlDataType.NVarChar, 255);
Column Column3 = new Column(TechOpsProjectTracker, "Project Description", dt3);
TechOpsProjectTracker.Columns.Add(Column3);

//Add Column Currently Assigned To
DataType dt4 = new DataType(SqlDataType.NVarChar, 50);
Column Column4 = new Column(TechOpsProjectTracker, "Currently Assigned To", dt4);
TechOpsProjectTracker.Columns.Add(Column4);
TechOpsProjectTracker.Create();

string connectionString = "Server=GSOPS4;Database=MyDatabaseName;Trusted_Connection=True;";

System.Data.SqlClient.SqlConnection sqlConnection1 = 
  new System.Data.SqlClient.SqlConnection(connectionString);

System.Data.SqlClient.SqlCommand cmd = 
  new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT TechOpsProjectTracker (ID, Alignment, Project Name, Project Description, Currently Assigned To ) VALUES (@ID, @Alignment, @Project Name, @Project Description, @Currently Assigned To)";

SqlParameter ID = new SqlParameter("@ID", SqlDbType.Int);
ID.Value = 5;
cmd.Parameters.Add(ID);

SqlParameter Alignment = new SqlParameter("@Alignment", SqlDbType.NVarChar, 50);
Alignment.Value = "Right";
cmd.Parameters.Add(Alignment);

SqlParameter ProjectName = new SqlParameter("@Project Name", SqlDbType.NVarChar, 50);
ProjectName.Value = "Right Project Name";
cmd.Parameters.Add(ProjectName);

SqlParameter ProjectDesc = new SqlParameter("@Project Description", SqlDbType.NVarChar, 255);
ProjectDesc.Value = "Right Project Description";
cmd.Parameters.Add(ProjectDesc);

SqlParameter Assignment = new SqlParameter("@Alignment", SqlDbType.NVarChar, 50);
Assignment.Value = "corp\\shress2";
cmd.Parameters.Add(Assignment); 

cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();

我得到这些异常的cmd.ExecuteNonQuery(); 任何原因可能是什么原因造成的?TIA

4

1 回答 1

7

有几个问题。

您的INSERT语句在列名中有空格,因此您必须将它们括在括号中。
此外,参数名称中不能有空格,因此您需要将单词粘在一起。

cmd.CommandText = "INSERT TechOpsProjectTracker (ID, Alignment, [Project Name], [Project Description], [Currently Assigned To] ) VALUES (@ID, @Alignment, @ProjectName, @ProjectDescription, @CurrentlyAssignedTo)";

然后,当你添加参数时,同样的事情,把这些词粘在一起:

SqlParameter ProjectName = new SqlParameter("@ProjectName", SqlDbType.NVarChar, 50);
ProjectName.Value = "Right Project Name";
cmd.Parameters.Add(ProjectName);


SqlParameter ProjectDesc = new SqlParameter("@ProjectDescription", SqlDbType.NVarChar, 255);
ProjectDesc.Value = "Right Project Description";
cmd.Parameters.Add(ProjectDesc);
于 2012-05-23T22:33:08.857 回答