C# 更新数据库
所以VisualStudio告诉我,我的引用在update语句中不正确。我觉得可能不止这些。我觉得我已经很接近了,但我看不出在这个sql语句中我错在哪里。网页的重点是更新数据库,这是这一步的全部内容。有人能帮帮我吗 这是我的密码 另外,我做了一个类似的insert语句,但是字符串idString部分一直到softwareReportRecord.Close();在update语句下面,它工作了C# 更新数据库,c#,asp.net,ado.net,sql-server-express,C#,Asp.net,Ado.net,Sql Server Express,所以VisualStudio告诉我,我的引用在update语句中不正确。我觉得可能不止这些。我觉得我已经很接近了,但我看不出在这个sql语句中我错在哪里。网页的重点是更新数据库,这是这一步的全部内容。有人能帮帮我吗 这是我的密码 另外,我做了一个类似的insert语句,但是字符串idString部分一直到softwareReportRecord.Close();在update语句下面,它工作了 using System; using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
reportDateText.Text = DateTime.Today.ToShortDateString();
//code page 429
if (Page.IsPostBack)
{
Page.Validate();
if (Page.IsValid)
{
bugReportForm.Visible = false;
regMessage.Visible = true;
string typeOS = oSListbox.SelectedValue;
string reportDate = reportDateText.Text;
string hardware = hardwareText.Text;
string occurrence = occurrenceRadioButtonList.SelectedValue;
string shortDescription = shortDescriptionText.Text;
string longDescription = longDescriptionText.Text;
string actionsTaken = actionsTakenText.Text;
SqlConnection dbConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Integrated Security=true");
try
{
dbConnection.Open();
dbConnection.ChangeDatabase("BugsReport");
}
catch (SqlException exception)
{
if (exception.Number == 911)
{
SqlCommand sqlCommand = new SqlCommand("CREATE DATABASE BugsReport", dbConnection);
sqlCommand.ExecuteNonQuery();
regMessage.Text = "<p>Successfully created the database.</p>";
dbConnection.ChangeDatabase("BugsReport");
}
else
Response.Write("<p>Error code " + exception.Number
+ ": " + exception.Message + "</p>");
}
finally
{
regMessage.Text += "<p>Successfully selected the database.</p>";
}
try
{
string SQLString = "SELECT * FROM softwareLog";
SqlCommand checkIDTable = new SqlCommand(SQLString, dbConnection);
SqlDataReader idRecords = checkIDTable.ExecuteReader();
idRecords.Close();
}
catch (SqlException exception)
{
if (exception.Number == 208)
{
SqlCommand sqlCommand = new SqlCommand("CREATE TABLE softwareLog (reportID SMALLINT IDENTITY(100,1) PRIMARY KEY, typeOS VARCHAR(25), reportDate DATE, hardware VARCHAR(50), occurrence VARCHAR(15), shortDescription VARCHAR(100), longDescription VARCHAR(500), actionsTaken VARCHAR(25))", dbConnection);
sqlCommand.ExecuteNonQuery();
regMessage.Text += "<p>Successfully created the table.</p>";
}
else
regMessage.Text += "<p>Error code " + exception.Number
+ ": " + exception.Message + "</p>";
}
finally
{
string idString = "SELECT IDENT_CURRENT('softwareLog') AS reportID";
SqlCommand newID = new SqlCommand(idString, dbConnection);
SqlDataReader softwareReportRecord = newID.ExecuteReader();
softwareReportRecord.Read();
string reportID = Convert.ToString(softwareReportRecord["reportID"]);
softwareReportRecord.Close();
string editRecord = "UPDATE softwareLog SET "
+ "typeOS='" + typeOS + "', "
+ "reportDate='" + reportDate + "', "
+ "hardware='" + hardware + "' "
+ "occurrence='" + occurrence + "' "
+ "shortDescription='" + shortDescription + "' "
+ "longDescription='" + longDescription + "' "
+ "actionsTaken='" + actionsTaken + "' "
+ "WHERE reportID=" + reportID + ";";
SqlCommand sqlCommand = new SqlCommand(editRecord, dbConnection);
sqlCommand.ExecuteNonQuery();
}
dbConnection.Close();
}
}
}
}
finally
{
string addRecord = "INSERT INTO softwareLog VALUES('"
+ typeOS + "', '"
+ reportDate + "', '"
+ hardware + "', '"
+ occurrence + "', '"
+ shortDescription + "', '"
+ longDescription + "', '"
+ actionsTaken + "')";
SqlCommand sqlCommand = new SqlCommand(addRecord, dbConnection);
sqlCommand.ExecuteNonQuery();
}
string idString = "SELECT IDENT_CURRENT('softwareLog') AS reportID";
SqlCommand newID = new SqlCommand(idString, dbConnection);
SqlDataReader softwareReportRecord = newID.ExecuteReader();
softwareReportRecord.Read();
string reportID = Convert.ToString(softwareReportRecord["reportID"]);
softwareReportRecord.Close();
regMessage.Text += "<p>Sorry for your inconvience. We will be working on your problem ASAP. For reference your ID is </p>" + reportID;
dbConnection.Close();
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Data.SqlClient;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
reportDateText.Text=DateTime.Today.ToSortDateString();
//代码页429
如果(第IsPostBack页)
{
Page.Validate();
如果(第页有效)
{
bugReportForm.Visible=false;
regMessage.Visible=true;
字符串typeOS=oSListbox.SelectedValue;
字符串reportDate=reportDateText.Text;
字符串hardware=hardwareText.Text;
字符串出现率=occurrenceRadioButtonList.SelectedValue;
string shortDescription=shortdescriptionontext.Text;
字符串longDescription=longDescriptionContext.Text;
字符串actionsTaken=actionsTakenText.Text;
SqlConnection dbConnection=newsqlconnection(“数据源=。\\SQLEXPRESS;集成安全性=true”);
尝试
{
dbConnection.Open();
dbConnection.ChangeDatabase(“BugsReport”);
}
捕获(SqlException异常)
{
如果(异常编号==911)
{
SqlCommand SqlCommand=newsqlcommand(“创建数据库错误报告”,dbConnection);
sqlCommand.ExecuteNonQuery();
regMessage.Text=“已成功创建数据库。”;
dbConnection.ChangeDatabase(“BugsReport”);
}
其他的
响应。写入(“错误代码”+异常。编号
+“:“+异常.消息+””;
}
最后
{
regMessage.Text+=“成功选择了数据库。”;
}
尝试
{
string SQLString=“从软件日志中选择*”;
SqlCommand checkIDTable=新的SqlCommand(SQLString,dbConnection);
SqlDataReader idRecords=checkIDTable.ExecuteReader();
idRecords.Close();
}
捕获(SqlException异常)
{
如果(exception.Number==208)
{
SqlCommand SqlCommand=new SqlCommand(“创建表softwareLog(reportID SMALLINT-IDENTITY(100,1)主键,typeOS-VARCHAR(25),reportDate-DATE,hardware-VARCHAR(50),occurrence-VARCHAR(15),shortDescription-VARCHAR(100),longsdescription-VARCHAR(500),actionsTaken-VARCHAR(25)),dbConnection);
sqlCommand.ExecuteNonQuery();
regMessage.Text+=“成功创建了表。”;
}
其他的
regMessage.Text+=“错误代码”+异常。编号
+“:”+异常。消息+“”;
}
最后
{
string idString=“选择当前标识('softwareLog')作为报告ID”;
SqlCommand newID=newsqlcommand(idString,dbConnection);
SqlDataReader softwareReportRecord=newID.ExecuteReader();
softwarereptrecord.Read();
string reportID=Convert.ToString(softwareReportRecord[“reportID”]);
softwarereptrecord.Close();
string editRecord=“更新软件日志集”
+“typeOS=”“+typeOS+”,”
+“reportDate=”+reportDate+“,”
+“硬件=”“+硬件+””
+“出现次数='”+出现次数+“”
+“shortDescription=”“+shortDescription+””
+“longDescription=”+longDescription+“”
+“actionsTaken='”+actionsTaken+“”
+“其中reportID=“+reportID+”;”;
SqlCommand SqlCommand=新的SqlCommand(editRecord,dbConnection);
sqlCommand.ExecuteNonQuery();
}
dbConnection.Close();
}
}
}
}
最后
{
string addRecord=“插入到软件日志值(”)
+typeOS+“,”
+报告日期+“,”
+硬件+“,”
+出现次数+“,”
+简短描述+“,”
+longDescription+“,”
+actionsTaken+“')”;
SqlCommand SqlCommand=新的SqlCommand(addRecord,dbConnection);
sqlCommand.ExecuteNonQuery();
}
string idString=“选择当前标识('softwareLog')作为报告ID”;
SqlCommand newID=newsqlcommand(idString,dbConnection);
SqlDataReader softwareReportRecord=newID.ExecuteReader();
softwarereptrecord.Read();
string reportID=Convert.ToString(softwareReportRecord[“reportID”]);
softwarereptrecord.Close();
regMessage.Text+=“很抱歉您的失礼
string editRecord = "UPDATE softwareLog SET "
+ "typeOS='" + typeOS.Replace("'", "''") + "', "
+ "reportDate='" + reportDate + "', "
+ "hardware='" + hardware.Replace("'", "''") + "',"
+ "occurrence='" + occurrence.Replace("'", "''") + "',"
+ "shortDescription='" + shortDescription.Replace("'", "''") + "',"
+ "longDescription='" + longDescription + "',"
+ "actionsTaken='" + actionsTaken.Replace("'", "''") + "'"
+ "WHERE reportID= " + reportID ;
string addRecord = "INSERT INTO softwareLog VALUES('"
+ typeOS.Replace("'", "''") + "', '"
+ reportDate + "', '"
+ hardware.Replace("'", "''") + "', '"
+ occurrence.Replace("'", "''") + "', '"
+ shortDescription.Replace("'", "''") + "', '"
+ longDescription.Replace("'", "''") + "', '"
+ actionsTaken.Replace("'", "''") + "')";
string editRecord = "UPDATE softwareLog SET "
+ "typeOS='" + typeOS + "', "
+ "reportDate='" + reportDate + "', "
+ "hardware='" + hardware + "',"
+ "occurrence='" + occurrence + "',"
+ "shortDescription='" + shortDescription + "',"
+ "longDescription='" + longDescription + "',"
+ "actionsTaken='" + actionsTaken + "'"
+ "WHERE reportID=" + reportID + "";