postgresql在c#中插入查询 [英] postgresql insert query in c#

查看:285
本文介绍了postgresql在c#中插入查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文本框的文本插入postgreql数据库的列中

我的代码是......

how to insert text of textbox into column in postgreql database
my code is...

string connectionString = "Server=localhost;port=5433;Database=raghv;User               ID=postgres;Password=ragh";

            NpgsqlConnection dbcon = new NpgsqlConnection(connectionString);
            dbcon.Open();

           NpgsqlCommand dbcmd = dbcon.CreateCommand();


            try
            {
                string sql1 = "INSERT INTO ramu(data) VALUES ('" + textBox3.Text.ToString () + "')";

                dbcmd.CommandText = sql1;
                dbcmd.Parameters.Add("@textBox3.Text", textBox3.Text.Trim());
                dbcmd.ExecuteNonQuery();
            }
            catch (NpgsqlException ex)
            { MessageBox.Show("there is a error");
            if (ex.Data == null)
            {
                throw;
            }
            else
            {
               
            }
            }



它抛出运行时异常请任何人建议我。


it throws runtime exception please anyone suggest me.

推荐答案

dbcmd.Parameters.Add(@ textBox3.Text,textBox3.Text .Trim());

u可以尝试将此行更改为;

dbcmd.Parameters.Add(@ textBox3,textBox3.Text.Trim());

和附加;

string sql1 =INSERT INTO ramu(data)VALUES(@ textBox3);
dbcmd.Parameters.Add("@textBox3.Text", textBox3.Text.Trim());
u can try with changing this line to;
dbcmd.Parameters.Add("@textBox3", textBox3.Text.Trim());
and additively;
string sql1 = "INSERT INTO ramu(data) VALUES (@textBox3)";


尝试以下:

Try the following:
...
            try
            {
                string sql1 = "INSERT INTO ramu(data) VALUES ('" + textBox3.Text.ToString () + "')";
 
                dbcmd.CommandText = sql1;
                // commented the line below
                //dbcmd.Parameters.Add("@textBox3.Text", textBox3.Text.Trim());
                dbcmd.ExecuteNonQuery();
            }
...


这篇关于postgresql在c#中插入查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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