如何在将记录插入数据库时​​删除空格 [英] how to remove blank space when inserting the record to database

查看:50
本文介绍了如何在将记录插入数据库时​​删除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在gridview记录如下



In gridview record as follows

Schdate      course   session      subject                Faculty
 28aug       GP 60      1         GSK P 2.1-2.5 26/42       AD
 28aug       GP 60      2         GSK P 2.1-2.5 27/42       AD
 28aug       GP 60      3         GSK P 2.1-2.5 28/42       AD
 28aug       GP 60      4         GSK P 2.1-2.5 29/42       AD
 28aug       GP 60      5         GSK P 2.1-2.5 30/42       AD





I我在数据库中插入以上记录



按如下方式插入按钮代码





I am inserting the above record in database

Insert button code as follows

protected void btn_insert_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow g1 in GridView1.Rows)
            {
                SqlConnection con = new SqlConnection(connStr);
                com = new SqlCommand("insert into Tb_Sch_Timetable(Schdate,course,Session,Subject,Faculty) values

('" + g1.Cells[0].Text.tostirng().trim() + "',
 '" + g2.Cells[1].Text.tostirng().trim() + "',
 '" + g3.cells[2].text.tostring().trim() + "',
 '" + g4.cells[3].text.tostring().trim() + "')",con);
             con.Open();
                com.ExecuteNonQuery();
                con.Close();

            }
            Label1.Text = "Records inserted successfully";
        }
    }
}





在数据库记录中如下



In database records as follows

Schdate      course   session      subject                               Faculty
 28aug       GP 60      1         GSK     P       2.1-2.5     26/42       AD
 28aug       GP 60      2         GSK     P       2.1-2.5     27/42       AD
 28aug       GP 60      3         GSK     P       2.1-2.5     28/42       AD
 28aug       GP 60      4         GSK     P       2.1-2.5     29/42       AD
 28aug       GP 60      5         GSK     P       2.1-2.5     30/42       AD





在gridveiw中,主题空白区域没有正确显示,



但是在数据库空白处插入记录时主题是为什么?



有什么问题?



请帮帮我。



添加了代码块 - OriginalGriff [/ edit]



in gridveiw for subject blank space is not there is displaying correctly,

but while inserting the records in database blank space is the in the subject why?

what is the problem?

please help me.

[edit]Code block added - OriginalGriff[/edit]

推荐答案

要做的第一件事是检查你的代码:要么不会compile( tostirng 不是方法, ToString 是; text 不是属性, Text 是; trim 不是方法,修剪是)或者你没有复制并粘贴它。



如果您复制并粘贴它,那么它没有编译,所以任何运行的都是早期版本,可能没有包含修剪说明。



另外,使用调试器查看数据:您需要准确了解单元格中的内容以及插入到数据库中的确切内容。可能是那些不是空格,而是Tabs - 在这种情况下,你需要使用Trim重载来指定要删除的字符。你还应该知道Trim只删除字符串末尾的字符,所以如果你的单元格数据有

The first thing to do is check your code: that either won't compile (tostirng is not a method, ToString is; text is not a property, Text is; trim is not a method, Trim is) or you didn't copy and paste it.

If you copy and pasted it, then it didn't compile, so anything that did run was an earlier version, which probably didn't include the trim instructions.

Other wise, look at your data using the debugger: you need to know exactly what is in the cells and exactly what is inserted into the DB. It may be that those aren't spaces, but Tabs - in which case you need to use the Trim overload that lets you specify which characters to remove. You should also be aware that Trim only removes characters at the ends of strings, so if your cell data has
"aa         bb"

它修剪完全不会改变。



请为此,请不要那样做!

Do不连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

it will not be changed at all by Trim.

And please, for your own sake, don't do it like that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


这篇关于如何在将记录插入数据库时​​删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆