使用C#代码(数据集)将URL存储在SQLSERVER 2005中 [英] Storing the url in SQLSERVER 2005 using C# code(data sets)

查看:91
本文介绍了使用C#代码(数据集)将URL存储在SQLSERVER 2005中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码存储url按钮点击。没有错误,但是所需的url不是在我的列字段(我使用ntext数据tpe为此)。请帮助我,如果有一些我的代码错误

  public partial class _Default:System.Web.UI.Page 
{
protected void Page_Load(object sender,EventArgs e)
{

}

public void Storetxt(String txt)
{


//连接到数据库

string connection =Data Source = .\\\sqlexpress2005;初始目录= PtsKuratlas; Integrated Security = True;
SqlConnection conn = new SqlConnection(connection);

//存储和操作数据的数据集对象

DataSet myDataSet = new DataSet();

//数据适配器执行SQL

SqlDataAdapter myDataAdapter = new SqlDataAdapter(SELECT * FROM gti_analytics,conn);
myDataAdapter.Fill(myDataSet,gti_analytics);
myDataAdapter.InsertCommand = new SqlCommand(INSERT INTO gti_analytics [links] VALUES [txt]);




}

protected void Button1_Click(object sender,EventArgs e)
{
字符串文本=http://google.com;
Storetxt(文本);
}
}


解决方案

问题是你实际上并没有对数据库执行命令。您正在定义要使用的InsertCommand,但是它没有被执行。



根据该代码,我看不到您需要使用DataAdapter / DataSet只需使用SqlCommand来做插件,这是更轻量级的。这样的东西:

  public void Storetxt(String txt)
{
//连接到数据库
string connection =Data Source = .\\\sqlexpress2005; Initial Catalog = PtsKuratlas; Integrated Security = True;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
conn = new SqlConnection(connection);
cmd = new SqlCommand(INSERT INTO gti_analytics(Links)VALUES(@Link),conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue(@ Link,txt);
conn.Open();
cmd.ExecuteNonQuery();
}
catch {//处理异常}
finally
{
if(cmd!= null)cmd.Dispose();
if(conn!= null)
{
if(conn.State == ConnectionState.Open)conn.Close();
conn.Dispose();
}
}
}

我也建议不要在你的数据库中使用ntext。如果你真的需要unicode支持,那么可以使用nvarchar,它最多可以达到4000个chars pre-sql 2005,或者nvarchar(max),它可以存储与SQL 2005相同的ntext。如果您不需要unicode支持,则使用varchar(8000字符前sql 2005,VARCHAR(MAX)从SQL 2005开始允许与文本相同)


I was trying to store the url on button click using the following code.There were no error but the required url is not sroeing in my column field (i used ntext data tpe for this).Please help me if there was some mistake in my code

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void Storetxt(String txt)
    {


        //connection to the database

        string connection = "Data Source=.\\sqlexpress2005;Initial Catalog=PtsKuratlas;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connection);

       //dataset object to store and manipulating data

        DataSet myDataSet = new DataSet();

        //data adapters to execute SQL

        SqlDataAdapter myDataAdapter = new SqlDataAdapter("SELECT * FROM gti_analytics", conn);
        myDataAdapter.Fill(myDataSet, "gti_analytics");
        myDataAdapter.InsertCommand = new SqlCommand("INSERT INTO gti_analytics [links] VALUES [txt]");




    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        String text = "http://google.com";
        Storetxt(text);
    }
}

解决方案

The problem is that you're not actually executing the command against the database. You're defining the InsertCommand to use, but it's not being executed.

Based on that code, I don't see that you need to use a DataAdapter/DataSet anyway, just use an SqlCommand to do the insert, which is more lightweight. Something like this:

public void Storetxt(String txt)
{
    //connection to the database
    string connection = "Data Source=.\\sqlexpress2005;Initial Catalog=PtsKuratlas;Integrated Security=True";
    SqlConnection conn = null;
    SqlCommand cmd = null;
    try
    {
        conn = new SqlConnection(connection);
        cmd = new SqlCommand("INSERT INTO gti_analytics (Links) VALUES (@Link)", conn);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@Link", txt);
        conn.Open();
        cmd.ExecuteNonQuery();
    }
    catch{//handle exceptions}
    finally
    {
        if (cmd != null) cmd.Dispose();
        if (conn != null) 
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
        }
    }
}

I'd also recommend not using ntext for this in your db. If you really need unicode support, use nvarchar which can go up to 4000 chars pre-sql 2005, or nvarchar(max) which can store as much as ntext from SQL 2005 onwards. If you don't need unicode support, use varchar instead (8000 chars pre-sql 2005, VARCHAR(MAX) from SQL 2005 onwards allows same as text)

这篇关于使用C#代码(数据集)将URL存储在SQLSERVER 2005中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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