我在编写从几个不同类收集的更新语句时遇到问题。所以让我解释一下我有什么..
我有一个 Minutes.cs 类,它将获取和设置数据库的属性
public class Minute{
public Minute()
{
//
// TODO: Add constructor logic here
//
}
public int ID
{
get;
set;
}
public string Location
{
get; set;
}
public string PeriodDate
{
get;
set;
}
public string PeriodTime
{
get;
set;
}
public string Facilitator
{
get;
set;
}
public string Attenders
{
get;
set;
}
public string Agenda
{
get; set;
}
public string Title
{
get; set;
}
public DateTime DateTime
{
get;
set;
}}
这是与数据库层交互的 MinuteDB.cs 类,我想将参数(SAParameters)从此类传递到 aspx.cs 页面,以便我可以进行参数化更新(唯一的原因是因为我无法更新日期时间格式,所以我必须使用参数化查询:S),我不知道我这样做是否正确。
public class MinuteDB{
//string strConnString = ConfigurationManager.ConnectionStrings["emaDB2"].ConnectionString;
public static String DB_STR = ConfigurationManager.ConnectionStrings["emaDB2"].ConnectionString;
//public static String DB_STR = "UserID=dba;Password=sql;DatabaseName=emaDB;ServerName=emaDB";
public MinuteDB()
{
}
public static void DoUpdateQuery(String sql)
{
//Create a connection, replace the data source name with the name of the SQL Anywhere Demo Database that you installed
SAConnection myConnection = new SAConnection(DB_STR);
//open the connection
myConnection.Open();
//Create a command object.
SACommand myCommand = myConnection.CreateCommand();
//Specify a query.
myCommand.CommandText = sql;
SAParameter parameters = new SAParameter();
parameters.SADbType = SADbType.NVarChar;
myCommand.Parameters.Add(parameters);
parameters = new SAParameter();
parameters.SADbType = SADbType.NVarChar;
myCommand.Parameters.Add(parameters);
parameters = new SAParameter();
parameters.SADbType = SADbType.NVarChar;
myCommand.Parameters.Add(parameters);
parameters = new SAParameter();
parameters.SADbType = SADbType.NVarChar;
myCommand.Parameters.Add(parameters);
parameters = new SAParameter();
parameters.SADbType = SADbType.NVarChar;
myCommand.Parameters.Add(parameters);
parameters = new SAParameter();
parameters.SADbType = SADbType.NVarChar;
myCommand.Parameters.Add(parameters);
parameters = new SAParameter();
parameters.SADbType = SADbType.Integer;
myCommand.Parameters.Add(parameters);
parameters = new SAParameter();
parameters.SADbType = SADbType.DateTime;
myCommand.Parameters.Add(parameters);
Minute recentMinutes = new Minute();
myCommand.Parameters[0].Value = recentMinutes.Title;
myCommand.Parameters[1].Value = recentMinutes.Location;
myCommand.Parameters[2].Value = recentMinutes.PeriodDate;
myCommand.Parameters[3].Value = recentMinutes.PeriodTime;
myCommand.Parameters[4].Value = recentMinutes.Attenders;
myCommand.Parameters[5].Value = recentMinutes.Agenda;
myCommand.Parameters[6].Value = recentMinutes.ID;
myCommand.Parameters[7].Value = recentMinutes.DateTime;
try
{
myCommand.ExecuteNonQuery();
}
catch (Exception excp)
{
throw excp;
}
finally
{
myConnection.Close();
}
}
public static void UpdateMinutesByMinutesID(int minuteID, string location, string title, string perioddate, string periodtime, string attenders, string agenda,DateTime datetime)
{
string sql = "UPDATE meetingMinutes SET title=?,location=?,perioddate=?,periodtime=?,attenders=?,agenda=?,datetime=? WHERE minuteID = " + minuteID;
DoUpdateQuery(sql);
}
}
这是我的 aspx.cs 类,单击一个按钮,它将更新到数据库
protected void btnUpdate_Click(object sender, EventArgs e)
{
int minID = Convert.ToInt32(Session["minID"]);
string dateAndTime = tbDatepicker.Text + " " + tbTimepicker.Text;
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime theDateTime = DateTime.ParseExact(dateAndTime, "d MMMM yyyy hh:mm tt", provider);
Minute min = new Minute();
min.DateTime = theDateTime;
try
{
MinuteDB.UpdateMinutesByMinutesID(minID,tbLocation.Text,tbTitle.Text,tbDatepicker.Text,tbTimepicker.Text,tbAttenders.Text,tbAgenda.Text,theDateTime);
}
finally
{
btnUpdate.Attributes.Add("onclick", "displaySuccessfulUpdate();");
Response.Redirect("HomePage.aspx");
}
}
我不知道为什么我不能让它工作。它提示我一个错误,说找不到 Column(minID)。是因为我没有将实际参数传递给方法吗?有人可以帮我吗?
PS:我使用的是 SQL Anywhere 12