我有一个asp.net项目。这是我的数据库的创建脚本;
USE [asgdb01]
GO
/****** Object: Table [dbo].[info] Script Date: 01/02/2013 03:32:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[info](
[ID] [int] IDENTITY(1,1) NOT NULL,
[NAME] [varchar](250) NULL,
[WORKTYPE] [varchar](250) NULL,
[MONDAY] [int] NULL,
[TUESDAY] [int] NULL,
[WEDNESDAY] [int] NULL,
[THURSDAY] [int] NULL,
[SATURDAY] [int] NULL,
[CUMARTESI] [int] NULL,
[SUNDAY] [int] NULL,
[TOTAL] [int] NULL,
[DATE] [date] NULL,
[RAWTIME] [int] NULL,
[DAY] [int] NULL,
[PENALTY1] [int] NULL,
[PENALTY2] [int] NULL,
[PENALTY_MINS] [int] NULL,
[CORRECTION] [int] NULL,
[CORRECTION_HOURS] [int] NULL,
[TOTAL_RAWTIME] [int] NULL,
[ACCUMULATIVE] [int] NULL,
CONSTRAINT [PK_info] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
你可以很容易地理解我想要做什么。 这是此页面的.cs代码;
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand cmd = new SqlCommand();
string strSQL = "UPDATE info (ID,DAYS,PENALTY1,PENALTY2,CORRECTION,DATE) Values (@ID,@DAYS,@PENALTY1,@PENALTY2,@CORRECTION,@DATE)";
string bag_str = WebConfigurationManager.ConnectionStrings["asgdb01ConnectionString"].ConnectionString;
conn = new SqlConnection(bag_str);
conn.Open();
cmd.Connection = conn;
cmd.CommandText = strSQL;
cmd.Parameters.Add(new SqlParameter("@ID", Int32.Parse(DropDownList1.SelectedItem.Value)));
cmd.Parameters.Add(new SqlParameter("@DATE", CalendarExtender1.SelectedDate));
cmd.Parameters.Add(new SqlParameter("@DAYS", Int32.Parse(TextBox3.Text)));
cmd.Parameters.Add(new SqlParameter("@PENALTY1", Int32.Parse(TextBox4.Text)));
cmd.Parameters.Add(new SqlParameter("@PENALTY2", Int32.Parse(TextBox5.Text)));
cmd.Parameters.Add(new SqlParameter("@CORRECTION", Int32.Parse(TextBox6.Text)));
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
Response.Write("Success ! ");
else
Response.Write("Error ! Try Again");
}
正如您从代码中看到的,我将从日历扩展器中选择2个日期,我将从下拉列表中选择员工,我将在我的文本框中输入值,然后我将点击“计算”按钮,我将更新所选我的数据库中的行。
我需要你帮助我的朋友们。 用四只眼睛等你的答案。 谢谢!
答案 0 :(得分:6)
您的UPDATE
语法错误。它应该是这样的
string strSQL = @"UPDATE info
SET DAYS = @DAYS,
PENALTY1 = @PENALTY1,
PENALTY2 = @PENALTY2,
CORRECTION = @CORRECTION,
DATE = @DATE
WHERE ID = @ID";
其他问题
你需要
using
语句tr-catch
阻止例如
string strSQL = @"UPDATE info
SET DAYS = @DAYS,
PENALTY1 = @PENALTY1,
PENALTY2 = @PENALTY2,
CORRECTION = @CORRECTION,
DATE = @DATE
WHERE ID = @ID";
string connStr = WebConfigurationManager.ConnectionStrings["asgdb01ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandText = strSQL;
comm.CommandType = CommandType.Text;
comm.Parameters.Add(new SqlParameter("@ID", Int32.Parse(DropDownList1.SelectedItem.Value)));
comm.Parameters.Add(new SqlParameter("@DATE", CalendarExtender1.SelectedDate));
comm.Parameters.Add(new SqlParameter("@DAYS", Int32.Parse(TextBox3.Text)));
comm.Parameters.Add(new SqlParameter("@PENALTY1", Int32.Parse(TextBox4.Text)));
comm.Parameters.Add(new SqlParameter("@PENALTY2", Int32.Parse(TextBox5.Text)));
comm.Parameters.Add(new SqlParameter("@CORRECTION", Int32.Parse(TextBox6.Text)));
try
{
conn.Open();
int i = comm.ExecuteNonQuery();
if (i > 0)
{
Response.Write("Success ! ");
}
else
{
Response.Write("No record was updated ");
}
}
catch(SqlException ex)
{
Response.Write(ex.ToString());
}
}
}