c#在一段时间内将递增的数字写入数据库

c#在一段时间内将递增的数字写入数据库,c#,C#,我试图写“增加的数字/价格”在一段时间内与增量在我的数据库,但仍然没有工作 没有一段时间,我的其他代码运行得很好,我在数据库中得到了写入的数据,但“while code”并没有。。。有人能帮我吗?谢谢 namespace Testing { class Program { static void Main(string[] args) { SqlConnection con; string str;

我试图写“增加的数字/价格”在一段时间内与增量在我的数据库,但仍然没有工作

没有一段时间,我的其他代码运行得很好,我在数据库中得到了写入的数据,但“while code”并没有。。。有人能帮我吗?谢谢

namespace Testing
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection con;
            string str;
            string Buyable;
            Buyable = "0";
            int count = 10; 
            double Add = 0.00000000;

            for (int i = 0; i < count; i++)
            {
                Add = Add + 0.00000005;    
            try
             {
                str = @"..........";
                con = new SqlConnection(str);
                con.Open();
                Console.WriteLine("Database connected");
                string query = "INSERT INTO[dbo].[Table]([Price], [Buyable]) VALUES('" + Add + "'," + Buyable + ")";
                SqlCommand ins = new SqlCommand(query, con);
                ins.ExecuteNonQuery();
                Console.WriteLine("Stored");
                Console.ReadKey();
            }
            catch (SqlException)
            {          
            }
          }
        }
    }
}
命名空间测试
{
班级计划
{
静态void Main(字符串[]参数)
{
SqlConnection-con;
字符串str;
字符串可购买;
Buyable=“0”;
整数计数=10;
双加=0.00000000;
for(int i=0;i
您正在编写con.Open();在循环中不关闭上一个连接con.Close();导致抛出错误的,写con.Close();在ins.ExecuteOnQuery()之后;试试看


另一种解决方案是在循环前打开连接,在循环结束后关闭连接。。我想这可能会帮助你

不要让连接保持打开状态,尝试使用
使用
来处理你生活中所有的
一次性
对象

试试这个:

static void Main(string[] args)
{
    double Add = 0D; //You really should use a **decimal** for anything to do with money!
    int Buyable = 0;
    int count = 10; 

    string str = @"..........";
    string sql = "INSERT INTO[dbo].[Table]([Price], [Buyable]) VALUES(@Add, @Buyable);";    // + Add + "'," + Buyable + ")";

    using (SqlConnection con = new SqlConnection(str))
    using (SqlCommand ins = new SqlCommand(sql, con))
    {
        ins.Parameters.Add("@Add", SqlDbType.Float);
        ins.Parameters.Add("@Buyable", SqlDbType.Int); //guessing at parameter type here
        con.Open();
        Console.WriteLine("Database connected");

        for (int i = 0; i < count; i++)
        {
            Add += 0.00000005D;    
            try
            {
                ins.Parameters["@Add"].Value = Add;
                ins.Parameters["@Buyable"].Value = Buyable;
                ins.ExecuteNonQuery();

                Console.WriteLine("Stored");
                Console.ReadKey();
            }
            catch (SqlException ex)
            { 
                //Do *something* with the exception here!
                Console.WriteLine("Error using the database. The message is:\n{0}", ex.Message);         
            }
        }
    }
}
static void Main(字符串[]args)
{
double Add=0D;//你真的应该用**十进制**来表示任何与钱有关的东西!
int可购买=0;
整数计数=10;
字符串str=@“……”;
string sql=“插入[dbo].[Table]([Price],[Buyable])值(@Add,@Buyable);”;//+Add+”,“+Buyable+”);
使用(SqlConnection con=newsqlconnection(str))
使用(SqlCommand-ins=newsqlcommand(sql,con))
{
ins.Parameters.Add(“@Add”,SqlDbType.Float);
ins.Parameters.Add(“@Buyable”,SqlDbType.Int);//在这里猜测参数类型
con.Open();
Console.WriteLine(“已连接数据库”);
for(int i=0;i
要做的第一件事是删除空的try/catch。这样,您就看不到sql代码中是否有任何错误。第二点。告诉我们错误。您需要在每次迭代中关闭连接
ins.ExecuteNonQuery();con.Close()using
语句always.Gah。sql注入漏洞。它烧了我们@JoelCoehoorn不是这里真正的大问题,但是是的……为什么你的附加值周围有
?价格不是一个数字字段吗?如果Buyable是一个字符串,为什么它周围没有
”?那么不带引号传递的字符串呢?@Steve它是数字的。对我来说,这看起来像一个int甚至smallint列。不过,这个答案还有其他问题。(延续不好的参数实践是主要的,但是继续强制为每个循环迭代创建新的连接对象也不好。@JoelCoehoorn我喜欢你的答案,所以我同意你的答案。是的,这是可能的,因为他说没有循环它是有效的,但是这个问题确实充满了问题。谢谢你,我的朋友,但是我无法工作,因为我收到下一个错误*,您发布的代码为//ExecuteOnQuery:Connection属性尚未初始化。现在应该更好了。
static void Main(string[] args)
{
    double Add = 0D; //You really should use a **decimal** for anything to do with money!
    int Buyable = 0;
    int count = 10; 

    string str = @"..........";
    string sql = "INSERT INTO[dbo].[Table]([Price], [Buyable]) VALUES(@Add, @Buyable);";    // + Add + "'," + Buyable + ")";

    using (SqlConnection con = new SqlConnection(str))
    using (SqlCommand ins = new SqlCommand(sql, con))
    {
        ins.Parameters.Add("@Add", SqlDbType.Float);
        ins.Parameters.Add("@Buyable", SqlDbType.Int); //guessing at parameter type here
        con.Open();
        Console.WriteLine("Database connected");

        for (int i = 0; i < count; i++)
        {
            Add += 0.00000005D;    
            try
            {
                ins.Parameters["@Add"].Value = Add;
                ins.Parameters["@Buyable"].Value = Buyable;
                ins.ExecuteNonQuery();

                Console.WriteLine("Stored");
                Console.ReadKey();
            }
            catch (SqlException ex)
            { 
                //Do *something* with the exception here!
                Console.WriteLine("Error using the database. The message is:\n{0}", ex.Message);         
            }
        }
    }
}