j1071287319 2016-05-06 16:57 采纳率: 50%
浏览 2067

关于winform控件添加数据库数据的,一直提示无法将char转换成money,能不能帮忙看看

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 游戏点卡管理
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    SqlConnection conn = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter DataAdapter = new SqlDataAdapter();
    DataSet ds = new DataSet();


    public void ff ()
    {
        try
        {
            dataGridView1.Columns.Clear();
            conn.Open();

            string str = string.Format("select cardid as 卡号,gamename as 游戏名称,company as 发行公司,lastday as 最后有效期,price as 售价 from cardinfo");
            cmd = new SqlCommand(str, conn);
            DataAdapter = new SqlDataAdapter();
            DataAdapter.SelectCommand = cmd;
            ds = new DataSet();

            DataAdapter.Fill(ds, "点卡");
            dataGridView1.DataSource = ds.Tables["点卡"];

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }

        finally
        {
            conn.Close();
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection("server=.;pwd=sa;uid=sa;database=GameCardDB;");
        ff();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        string id = this.textBox1.Text.Trim();
        string gs = this.textBox2.Text.Trim();
        int sj = int.Parse(this.textBox3.Text.Trim());
        string mc = this.textBox4.Text.Trim();
        string time = this.dateTimePicker1.Text.Trim();
        conn.Open();
        string str = String.Format("insert into cardinfo values('{0}','{1}',{2},'{3}')", mc, gs, time, sj);
        cmd = new SqlCommand(str, conn);

        try
        {

            int numSQLCount = cmd.ExecuteNonQuery();
            if (numSQLCount > 0)
            {
                MessageBox.Show("添加成功!");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }
        finally
        {
            conn.Close();

        }
        ff();

    }

    private void button2_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }
}

}

  • 写回答

4条回答 默认 最新

  • j1071287319 2016-05-06 16:57
    关注

    create database GameCardDB

    on primary
    (
    name='GameCardDB_data',
    filename='d:\GameCardDB_data.mdf',
    size=10mb,
    maxsize=20mb,
    filegrowth=10%
    )

    log on
    (
    name='GameCardDB_log',
    filename='d:\GameCardDB_log.ldf',
    size=10mb,
    maxsize=20mb,
    filegrowth=10%
    )
    go

    use GameCardDB
    go

    create table cardinfo
    (
    cardid int primary key identity(1,1),--卡号
    gamename varchar(50) not null,--游戏名称
    company varchar(50) not null,--发行公司
    lastday datetime not null,--最后有效期
    price money not null,--售价
    )
    go

    insert into cardinfo(gamename,company,lastday,price) values('梦想世界','忘了',2016-1-4,50)
    insert into cardinfo(gamename,company,lastday,price) values('英雄联盟','拳头',2016-5-7,100)
    insert into cardinfo(gamename,company,lastday,price) values('魔兽世界','暴雪',2016-2-3,150)

    这是数据库文件

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况