日期:2014-05-18 浏览次数:20589 次
create table Page ( PageID int identity(1, 1) not null primary key, PageUrl nvarchar(500) not null, PageTitle nvarchar(100) not null, CountOfClick int not null default 0 );
SqlConnection conn = null; SqlCommand cmd = null; try { conn = new SqlConnection("Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;"); conn.Open(); cmd = conn.CreateCommand(); cmd.CommandText = "insert into Page(PageUrl, PageTitle, CountOfClick) values(@PageUrl, @PageTitle, @CountOfClick)"; cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PageUrl", page.PageUrl)); cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PageTitle", page.PageTitle)); cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CountOfClick", 0)); cmd.ExecuteNonQuery(); //【注意】这里报错:参数化查询 '(@PageUrl nvarchar(13),@PageTitle nvarchar(10),@CountOfClick big' 需要参数 '@CountOfClick',但未提供该参数。 return true; } catch { return false; } finally { if (cmd != null) { cmd.Dispose(); } if (conn != null) { conn.Close(); conn.Dispose(); } }