我得到的sqlException是未處理的。 'nvarchar'附近的語法錯誤,'Name'附近語法不正確。我使用SMO來創建這樣C#插入數據到SQL表中
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
如果使用SQL Server Management Studio中的選項,可以隨時右鍵點擊該表(在對象瀏覽器中)並選擇「Script Table as ... Insert」,它將爲您生成適當的語法。 –
參數名稱中不能有空格,但這是因爲您的列有空格(它們不應該 - 雖然支持它是一場噩夢)。 –