如果文本框为空,尝试将SQL表中的日期时间字段设置为NULL,我似乎无法使其工作.
string EndDate = "";
if (String.IsNullOrEmpty(EndDateTxtBox.Text.Trim()))
{
EndDate = null;
}
else
{
EndDate = EndDateTxtBox.Text;
}
var sql = String.Format(@"UPDATE Test SET StartDate='{0}',
EndDate='{1}' WHERE ID = '{2}'",
StartDateTxtBox.Text, EndDate, id);
Run Code Online (Sandbox Code Playgroud)
当我这样做并提出一个断点时,我得到了"var sql":
"UPDATE Test SET StartDate='5/23/2013', EndDate=" WHERE ID = '19'"
Run Code Online (Sandbox Code Playgroud)
我尝试从sql字符串中删除'但是这也不起作用.有什么建议?
编辑:我理解防止SQL注入的重要性,但这是我内部Web服务器上的一个页面,仅供我使用而不是向公众投射.这是为了帮助我跟踪个人事物.
我正在运行一组数据,这将填充CheckBoxList.我想显示列表中的所有项目,但某些项目将被禁用,因为它们不符合条件(其状态等于非活动状态).如何在后面的代码中将项目设置为禁用(Enabled = false)?
if (node.SelectSingleNode("Status") == "Inactive")
{
customerList.Items.Add(new ListItem(displayName, displayValue)); // DISABLED
}
else
{
customerList.Items.Add(new ListItem(displayName, displayValue)); // ENABLED
}
Run Code Online (Sandbox Code Playgroud) 我需要一些关于如何更好地做到这一点的提示,我正在使用一个连接插入多个查询。
我知道这不是一个好的编程,特别是它很容易发生 SQL 注入,我还想提一下它不会在互联网上发布,只是在本地运行。
这是我到目前为止所拥有的..
public partial class Modify : System.Web.UI.Page
{
OleDbConnection connection;
OleDbCommand command;
public void OpenConnection2()
{
connection = new OleDbConnection("");
command = new OleDbCommand();
connection.Open();
}
protected void btnSave_Click1(object sender, EventArgs e)
{
if (AcctNumList.SelectedValue == "3")
{
string query2 = String.Format(@"INSERT INTO ACH (rptid, tableid, name, amount, stat, create_date) values
('{0}','{1}','{2}','{3}','{4}','{5}')",
id, newguid, Name1TxtBox.Text.Replace("'", "''"), Amt1TxtBox.Text.Replace("'", "''"), 3, DateTime.Now.ToString());
string query3 = String.Format(@"INSERT INTO ACH (rptid, tableid, name, amount, stat, create_date) values
('{0}','{1}','{2}','{3}','{4}','{5}')", …Run Code Online (Sandbox Code Playgroud) 我试图从后面的代码中添加一个OnClick属性到一个按钮.取决于Attending元素是否为0,将确定添加哪个OnClick属性.当我单击带有下面代码的按钮时,我收到以下错误:
"Microsoft JScript运行时错误:Sys.WebForms.PageRequestManagerServerErrorException:无效的回发或回调参数.使用配置或页面中的<%@ Page EnableEventValidation ="true"%>启用事件验证.出于安全考虑,此功能验证参数回发或回调事件源自最初呈现它们的服务器控件.如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证."
我究竟做错了什么?
ASPX
<%@ Page Title="" Language="C#" EnableEventValidation="true" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="test.CommunityEvents.Default" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DataList ID="DataList1" RepeatColumns="1" CellPadding="5" OnItemDataBound="Dl1_ItemDataBound"
runat="server">
<ItemTemplate>
<div id="Attendingbox" runat="server">
<asp:Label ID="AttendingorNot" runat="server"></asp:Label>
</div>
<br />
<asp:Button ID="SignupButton" runat="server" Text="" />
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)
代码背后
protected void Dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
.....//removed other code to save space
Button SignupButton = (Button)e.Item.FindControl("SignupButton");
if (Attending == 0)
{
AttendingorNot.Text = …Run Code Online (Sandbox Code Playgroud) 我有两个表Business和BusinessCat.我需要一个MSSQL查询,它将获得以下结果:
ID | CategoryName | RecordCount
1 | Local | 3
2 | National | 1
3 | International| 2
4 | Other | 0
Run Code Online (Sandbox Code Playgroud)
我需要计算每个类别中有多少作为另一列.
业务表看起来像这样:
ID | Category | BusinessName
1 | 3 | Blackstone, Inc.
2 | 2 | Pet Smart
3 | 1 | John Doe
4 | 3 | Best Buy
5 | 1 | Sams Treats
6 | 1 | Eastcoast Tattoo
Run Code Online (Sandbox Code Playgroud)
BusinessCat表如下所示:
ID | CategoryName
1 | Local
2 | National
3 | …Run Code Online (Sandbox Code Playgroud) 我很难过,我试图从数据库中填充5个文本框,而不是所有5个文本框都会有数据.
例:
ID | ItemID | QTYPE
1 | 10 | 2盒
2 | 10 | 6盒
3 | 11 | 1个案例
在这个例子中,它将填充QuantityType1TxtBox 2 Boxes和QuantityType2TxtBox 6 Boxes,而其他三个文本框留空.
我尝试运行此代码时得到的以下错误是:索引超出了数组的范围.
此行发生此错误:QuantityType2TxtBox.Text = rdr.GetString(1);
SqlCommand cmd = new SqlCommand(@"SELECT QType FROM InventoryQType
WHERE ItemID = '" + itemID + "'", conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
QuantityType1TxtBox.Text = rdr.GetString(0);
QuantityType2TxtBox.Text = rdr.GetString(1);
QuantityType3TxtBox.Text = rdr.GetString(2);
QuantityType4TxtBox.Text = rdr.GetString(3);
QuantityType5TxtBox.Text = rdr.GetString(4);
}
rdr.Close();
Run Code Online (Sandbox Code Playgroud) 我有一个包含大量电子邮件地址的数据库,我需要将它们格式化为一个字符串,如下所示:
toaddress = "name@gmail.com,name1@gmail.com,name2@gmail.com,name3@gmail.com"
Run Code Online (Sandbox Code Playgroud)
当前代码:
string toaddress = "";
using (SqlConnection conn = new SqlConnection(""))
{
SqlCommand cmd = new SqlCommand("SELECT email FROM dbo.Members", conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
toaddress = rdr["email"].ToString();
}
rdr.Close();
}
Run Code Online (Sandbox Code Playgroud) 我需要从字符串中提取票证号码。字符串示例:
string body = "Hello, world welcome. [TKNM-1234] Blah blah [HelpCode-5] Blah blah";
Run Code Online (Sandbox Code Playgroud)
我只需1234要从该字符串中提取。做这个的最好方式是什么。我以前尝试过:
int from = body.IndexOf("[TKNM-") + "[TKNM-".Length;
int to = body.LastIndexOf("]");
ticketNumber = body.Substring(from, to - from);
Run Code Online (Sandbox Code Playgroud)
但这是由于TKNM之后的其他括号而引起的问题。一切都在C#中。
c# ×7
sql ×4
asp.net ×2
sql-server ×2
button ×1
checkboxlist ×1
oledb ×1
onclick ×1
sql-insert ×1