Sel*_*ase 2 c# sql asp.net connection-string insert
那里有一些鹰眼的人吗?我想在下面的插入语句中删除错误的位置.我在运行时使用了断点来跟踪代码,并且我得到了异常错误(在关键字"Case"附近有一个不正确的语法).让你的老鹰眼睛......
public partial class OpenCase : System.Web.UI.Page
{
string adminString;
protected void Page_Load(object sender, EventArgs e)
{
adminString = "CA123";
}
protected void openCaseBotton_Click(object sender, EventArgs e)
{
//SQL connection string
SqlDataSource CSMDataSource = new SqlDataSource();
CSMDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString"].ToString();
//SQL Insert command with variables
CSMDataSource.InsertCommandType = SqlDataSourceCommandType.Text;
CSMDataSource.InsertCommand = "INSERT INTO Case (CaseID, CaseDesc, DateOpened, CasePriority, AdministratorID) VALUES (@CaseID, @CaseDesc, @DateOpened, @CasePriority, @AdministratorID)";
//Actual Insertion with values from textboxes into databse fields
CSMDataSource.InsertParameters.Add("CaseID", caseIDTextBox.Text);
CSMDataSource.InsertParameters.Add("CaseDesc", caseDescTextBox.Text);
CSMDataSource.InsertParameters.Add("DateOpened", DateTime.Now.ToString());
CSMDataSource.InsertParameters.Add("CasePriority", null);
CSMDataSource.InsertParameters.Add("AdministratorID", adminString.ToString());
int rowsCommitted = 0;
//Try catch method to catch exceptions during insert
try
{
rowsCommitted = CSMDataSource.Insert();
Response.Write(@"<script language='javascript'>alert('The following errors have occurred:');</script>");
}
catch (Exception ex)
{
//error message displayed when exception occurs
string script = "<script>alert('" + ex.Message + "');</script>";
Response.Write("The following Error occurred while entering the records into the database" + " " + ex.ToString() + " ");
Response.Redirect("~/ErrorPage.aspx", false);
}
finally
{
CSMDataSource = null;
}
//Where to go next if insert was successful or failed
if (rowsCommitted != 0)
{
Response.Redirect("~/CaseAdmin.aspx", false);
}
else
{
Response.Redirect("~/ErrorPage.aspx", false);
}
}
protected void addExhibitBotton_Click(object sender, EventArgs e)
{
Response.Redirect("~/EntryForms/AddExhibit.aspx", false);
}
}
Run Code Online (Sandbox Code Playgroud)
看到什么?可能你需要一个C#错误视力护目镜......哈哈
靠近INSERT?好吧,CASE是一个SQL保留字,所以试试简单:
CSMDataSource.InsertCommand = "INSERT INTO [Case] (...
Run Code Online (Sandbox Code Playgroud)
告诉它这Case是一个对象名,而不是SQL关键字.