if(txtAddress.Text!= string.Empty) if(txtAddress.Text!= string.Empty) { con.Close(); } 如果(doredirect) 回复.Redirect(" Login.aspx"); } lblMessage.Text ="修复以下错误并重试: " ;; } I''m running the following code in a C#.NET page and it doesn''t enter the values into the DB. I''m certain the problem is to do with the txtBirth field. It allows users to enter a DOB as dd/mm/yyyy and I think it''s the slashes(/) that are causing the problem. If I don''t enter a DOB in this field then all the data enters into the database without a problem. Any ideas? SQL Server 2000, VS.NET, C# if (Page.IsValid) { // Save the new user to the database SqlConnection con; string sql; SqlCommand cmd; StringBuilder sb = new StringBuilder(); ArrayList values = new ArrayList(); sb.Append("INSERT INTO [User] "); sb.Append("(UserID, Login, Password, FirstName, LastName, "); sb.Append("PhoneNumber, Email, IsAdministrator, Address, "); sb.Append("CellNumber, DateOfBirth) "); sb.Append("VALUES (''{0}'', ''{1}'', ''{2}'', ''{3}'', ''{4}'', ''{5}'', ''{6}'', ''{7}'', "); // Optional values without quotes as they can be null sb.Append("{8}, {9}, {10})"); // Add required values to replace values.Add(Guid.NewGuid().ToString()); values.Add(txtLogin.Text); values.Add(txtPwd.Text); values.Add(txtFName.Text); values.Add(txtLName.Text); values.Add(txtPhone.Text); values.Add(txtEmail.Text); values.Add(0); // Add the optional values or Null if (txtAddress.Text != string.Empty) values.Add("''" + txtAddress.Text + "''"); else values.Add("Null"); if (txtMobile.Text != string.Empty) values.Add("''" + txtMobile.Text + "''"); else values.Add("Null"); if (txtBirth.Text != string.Empty) values.Add("''" + txtBirth.Text + "''"); else values.Add("Null"); // Format the string with the array of values sql = String.Format(sb.ToString(), values.ToArray()); // Connect and execute the SQL con = new SqlConnection("data source=127.0.0.1;initial catalog=Friends;user id=sa;"); cmd = new SqlCommand(sql, con); con.Open(); bool doredirect=true; try { cmd.ExecuteNonQuery(); } catch { doredirect = false; this.lblMessage.Visible = true; //this.lblMessage.Text = "Insert couldn''t be performed. Username mayalready be taken."; this.lblMessage.Text = sql; } finally { con.Close(); } if (doredirect) Response.Redirect("Login.aspx"); } else lblMessage.Text = "Fix the following errors and retry:"; } Miha, 谢谢为你的inout。我不太清楚该怎么做。我是一个非常新的 到.NET并正在编写一本Wrox书 - 它实际上是他们的 教程中的一个导致我错误! 请你再详细解释一下。 谢谢 Miha Markic < miha at rthand com>在消息中写道 新闻:OE ************** @ tk2msftngp13.phx.gbl ...Miha,Thanks for your inout. I''m not too sure what to do with this. I''m quite newto .NET and am working through a Wrox book - it''s actually one of theirtutorials thats causing me the error!Would you mind giving a little more explanation please.Thanks"Miha Markic" <miha at rthand com> wrote in messagenews:OE**************@tk2msftngp13.phx.gbl...安德鲁, 您可以考虑使用参数化命令。请参阅SqlCommand.Parameters属性.. - Miha Markic - RightHand .NET咨询&开发 miha at rthand com www.rthand.com 安德鲁·班克斯 < BA **** @ nojunkblueyonder.co.uk>在消息中写道新闻:Hg ********************* @ news-text.cableinet.net ... Hi Andrew, You might consider using parametrised commands. See SqlCommand.Parameters property.. -- Miha Markic - RightHand .NET consulting & development miha at rthand com www.rthand.com "Andrew Banks" <ba****@nojunkblueyonder.co.uk> wrote in message news:Hg*********************@news-text.cableinet.net...我在C#.NET页面中运行以下代码,它没有输入值到DB中。我确定问题与txtBirth 字段有关。它允许用户输入DOB作为dd / mm / yyyy,我认为它是导致问题的斜杠(/)。如果我在这个字段中没有输入DOB,那么所有数据都会毫无问题地进入数据库。 任何想法? SQL Server 2000 ,VS.NET,C# 如果(Page.IsValid) //将新用户保存到数据库中 SqlConnection con; 字符串sql; SqlCommand cmd; StringBuilder sb = new StringBuilder(); ArrayList values = new ArrayList(); sb.Append(" INSERT INTO [User]"); sb.Append("(UserID) ,登录,密码,名字,姓氏,"); sb.Append(" PhoneNumber,Email,IsAdministrator,Address,"); sb.Append (CellNumber,DateOfBirth"); sb.Append(" VALUES('{0}'',''{1}'',''{2}'' ,''{3}'',''{4}'',''{5}'',''{6}'',''{7}'',"); //没有引号的可选值,因为它们可以为null 某人.Append(" {8},{9},{10})"); //添加所需的值来替换 values.Add(Guid。 NewGuid()。ToString()); values.Add(txtLogin.Text); values.Add(txtPwd.Text); values.Add(txtFName.Text); values.Add(txtLName.Text); values.Add(txtPhone.Text); values.Add(txtEmail.Text); values.Add(0); //添加可选值或Null if(txtAddress.Text!= string.Empty) values.Add("''" + txtAddress.Text +"''"); values.Add(" Null); if( txtMobile.Text!= string.Empty) values.Add("''" + txtMobile.Text +"''"); else values.Add(" Null); if(txtBirth.Text!= string.Empty) values.Add(" ''" + txtBirth.Text +"''"); values.Add(" Null"); 使用值数组格式化字符串 sql = String.Format(sb.ToString(),values.ToArray()); //连接并执行SQL con = new SqlConnection(" data source = 127.0.0.1; initial catalog = Friends; I''m running the following code in a C#.NET page and it doesn''t enter the values into the DB. I''m certain the problem is to do with the txtBirth field. It allows users to enter a DOB as dd/mm/yyyy and I think it''s the slashes(/) that are causing the problem. If I don''t enter a DOB in this field then all the data enters into the database without a problem. Any ideas? SQL Server 2000, VS.NET, C# if (Page.IsValid) { // Save the new user to the database SqlConnection con; string sql; SqlCommand cmd; StringBuilder sb = new StringBuilder(); ArrayList values = new ArrayList(); sb.Append("INSERT INTO [User] "); sb.Append("(UserID, Login, Password, FirstName, LastName, "); sb.Append("PhoneNumber, Email, IsAdministrator, Address, "); sb.Append("CellNumber, DateOfBirth) "); sb.Append("VALUES (''{0}'', ''{1}'', ''{2}'', ''{3}'', ''{4}'', ''{5}'', ''{6}'',''{7}'', "); // Optional values without quotes as they can be null sb.Append("{8}, {9}, {10})"); // Add required values to replace values.Add(Guid.NewGuid().ToString()); values.Add(txtLogin.Text); values.Add(txtPwd.Text); values.Add(txtFName.Text); values.Add(txtLName.Text); values.Add(txtPhone.Text); values.Add(txtEmail.Text); values.Add(0); // Add the optional values or Null if (txtAddress.Text != string.Empty) values.Add("''" + txtAddress.Text + "''"); else values.Add("Null"); if (txtMobile.Text != string.Empty) values.Add("''" + txtMobile.Text + "''"); else values.Add("Null"); if (txtBirth.Text != string.Empty) values.Add("''" + txtBirth.Text + "''"); else values.Add("Null"); // Format the string with the array of values sql = String.Format(sb.ToString(), values.ToArray()); // Connect and execute the SQL con = new SqlConnection("data source=127.0.0.1;initial catalog=Friends; user user id = sa;"); cmd = new SqlCommand(sql,con); con.Open(); bool doredirect = true ; 尝试 { cmd.ExecuteNonQuery(); } CATC h /> {doredirect = false; this.lblMessage.Visible = true; //这个。 lblMessage.Text ="无法执行插入。用户名 id=sa;"); cmd = new SqlCommand(sql, con); con.Open(); bool doredirect=true; try { cmd.ExecuteNonQuery(); } catch { doredirect = false; this.lblMessage.Visible = true; //this.lblMessage.Text = "Insert couldn''t be performed. Username may 已经被采取。" ;; this.lblMessage.Text = sql; } 最后 { con.Close(); } 如果(doredirect) Response.Redirect(" Login.aspx"); } 其他 lblMessage .Text ="修正以下错误并重试:" ;; } be taken."; this.lblMessage.Text = sql; } finally { con.Close(); } if (doredirect) Response.Redirect("Login.aspx"); } else lblMessage.Text = "Fix the following errors and retry:"; } 这篇关于将日期插入SQL Server DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 22:07