如何将数据插入包含使用语句的Sql内置数据库? [英] How Can I Insert Data To Sql Inbuilt Database Including Using Statement?

查看:103
本文介绍了如何将数据插入包含使用语句的Sql内置数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 {
string name = PROMOD;

尝试
{
使用(SqlConnection cn = new SqlConnection( global :: EnQApp.Properties.Settings.Default.Database1ConnectionString))
{
SqlCommand cmd = new SqlCommand( INSERT INTO Inquiry(Uname,Cust_Name)VALUES(@ Uname,@ Cus_Name),cn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue( @ Uname,name);
cmd.Parameters.AddWithValue( @ Cus_Name,Cus_Name.Text);
//
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();

}

}
catch (Exception ex){}
最后 {}
}





i开发了一个插入数据的代码sql数据库。但它不会将任何数据添加到sql数据库中,也不会给出任何错误。



数据库包含3列



 [Id]  INT   NOT   NULL   IDENTITY 
[Uname] VARCHAR (MAX) NULL
[Cust_Name] VARCHAR (MAX) NULL





我做错了什么?

解决方案

我们无法告诉你问题在哪里:你需要使用调试器才能解决问题。

目前,你没有数据:只是缺乏数据库中的数据。首先收集信息!

稍微改变你的代码:

  string  name =   promod; 

尝试
{
string strConnect = < span class =code-keyword> global :: EnQApp.Properties.Settings.Default.Database1ConnectionString;
使用(SqlConnection cn = new SqlConnection(strConnect))
{
SqlCommand cmd = new SqlCommand( INSERT INTO Inquiry (Uname,Cust_Name)VALUES(@ Uname,@ Cus_Name),cn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue( @ Uname,name);
cmd.Parameters.AddWithValue( @ Cus_Name,Cus_Name.Text);
//
cn.Open();
int rowsAdded = cmd.ExecuteNonQuery();
cn.Close();

}

}
catch (例外情况)
{
MessageBox.Show(ex.Message);
}



并设置一个断点:

  string  name =   promod; 

运行你的应用程序,然后单步执行。

加载时检查连接字符串:看起来是否正确?是你认为的DB吗?它是正确的服务器吗?

一步一步,看看它发生了什么,并检查从ExecuteNonQuery返回的值。是1吗?你有例外吗?消息是什么?



在你拥有它之前,没有人可以帮助你 - 因为没有人知道它失败的地方,更不用说为什么了。 / BLOCKQUOTE>

{
            string name = "promod";
        
            try
            {
                using (SqlConnection cn = new SqlConnection(global::EnQApp.Properties.Settings.Default.Database1ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand("INSERT INTO Enquiry (Uname,Cust_Name) VALUES (@Uname,@Cus_Name)", cn);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@Uname", name);
                    cmd.Parameters.AddWithValue("@Cus_Name", Cus_Name.Text);
                    //
                    cn.Open();
                    cmd.ExecuteNonQuery();
                    cn.Close();     

                }

            }
            catch (Exception ex) { }
            finally { }
        }



i have developed a code for insert data into sql database. but it doesn't add any data into sql database and it wont give any errors as well.

database contains 3 columns

[Id]   INT   NOT NULL IDENTITY,
    [Uname]        VARCHAR (MAX) NULL,
    [Cust_Name]    VARCHAR (MAX) NULL,



what is the mistake i have done?

解决方案

We can't tell you where the problem lies: you need to use the debugger to work it out.
At the moment, you have no data: just a lack of data in a database. So start by gathering information!
Change your code a little:

string name = "promod";

try
{
    string strConnect = global::EnQApp.Properties.Settings.Default.Database1ConnectionString;
    using (SqlConnection cn = new SqlConnection(strConnect))
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO Enquiry (Uname,Cust_Name) VALUES (@Uname,@Cus_Name)", cn);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@Uname", name);
        cmd.Parameters.AddWithValue("@Cus_Name", Cus_Name.Text);
        //
        cn.Open();
        int rowsAdded = cmd.ExecuteNonQuery();
        cn.Close();

    }

}
catch (Exception ex) 
{
    MessageBox.Show(ex.Message);
}


And put a breakpoint on the line:

string name = "promod";

Run your application, and single step through.
Check the connection string when it loads: does it look right? Is it the DB you thought it was? Is it the right server?
Step through, looking at what it happening, and check what value is returned from ExecuteNonQuery. Is it 1? Do you get an exception? What is the message?

Until you have that, no-one can help you - because no-one has any idea where it is failing, let alone why.


这篇关于如何将数据插入包含使用语句的Sql内置数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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