首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据库读写代码流程设计的最佳方法

数据库读写代码流程设计的最佳方法
EN

Stack Overflow用户
提问于 2009-08-08 08:29:23
回答 2查看 216关注 0票数 1

因此,我正在尝试我的概念工具,在这里,我需要能够读写数据从一个真正的数据库非常容易。我根据自己的喜好设置了表单,并在不同的文本框和下拉列表中分布,以便从数据库中读取数据。我已经完成了所有的工作,但是有一个小错误,我不完全明白为什么会存在。有些文本框不更新数据库中的文本。但似乎只有当数据库中的数据为空时才会发生这种情况。因此,最后一行的值仍然挂在文本框中,因此,单击"Update“实际上会将字段中的值从最后一行更新到新行。把一切搞砸了。

现在,我最感兴趣的是代码的剪切流。做这一切的最好方法是什么?到目前为止我已经知道了:

以下是在datagridview中单击单元格时的代码:

代码语言:javascript
复制
Private Sub DataGridView_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellClick

    On Error Resume Next   

    selectedName = Me.DataGridView.CurrentRow.Cells(0).Value
    selectedGenre = Me.DataGridView.CurrentRow.Cells(1).Value
    selectedRhytm = Me.DataGridView.CurrentRow.Cells(2).Value
    selectedLength = Me.DataGridView.CurrentRow.Cells(3).Value
    selectedFinished = Me.DataGridView.CurrentRow.Cells(4).Value
    selectedSoundFile = Me.DataGridView.CurrentRow.Cells(5).Value

    txtBoxName.Text = selectedName
    txtBoxGenre.Text = selectedGenre
    txtBoxRhytm.Text = selectedRhytm
    txtBoxLength.Text = selectedLength
    txtBoxFinished.Text = selectedFinished
    txtBoxSoundFile.Text = selectedSoundFile

End Sub

“选定的”-variables都是在我已经创建的供以后使用的GlobalCode.vb中声明的。它们的定义如下:

代码语言:javascript
复制
Friend Module GlobalVariables

    Friend selectedName As String = Nothing
    Friend selectedGenre As String = Nothing
    Friend selectedRhytm As String = Nothing
    Friend selectedLength As String = Nothing
    Friend selectedFinished As String = Nothing
    Friend selectedSoundFile As String = Nothing


End Module

我以前从没做过这样的事。我更像是一个设计师,而不是程序员,但是我真的需要尝试一个概念,所以我不确定这是做这件事的方法。我发现它大部分时候都起作用了。但我认为,熟练的程序员有一种设计代码布局的方法,这样就可以高效、干净和易于阅读。这看上去怎么样?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-08-08 08:48:50

(我看不到问题中任何与数据库有关的东西,顺便说一下)

也许排列这段代码的最好方法是..。别说了。不要为标准数据绑定框架能够处理的事情编写代码。例如(对不起是C#,但它应该翻译-这里所有的“好”位都是由.NET框架提供的,而不是语言提供的);一些UI代码--注意没有复制值的代码:

代码语言:javascript
复制
static class Program {
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        // some sample data
        BindingList<Track> tracks = new BindingList<Track>();
        tracks.Add(new Track { Name = "foo", Genre = "Rock", Rhythm = "insane", Length = 180 });
        tracks.Add(new Track { Name = "bar", Genre = "Classic", Rhythm = "sedate", Length = 240 });

        // show the data on a form
        using (Form form = new Form {
            Controls = {
                new DataGridView { DataSource = tracks, Dock = DockStyle.Fill },
                new TextBox { DataBindings = {{"Text", tracks, "Name"}}, Dock = DockStyle.Bottom},
                new TextBox { DataBindings = {{"Text", tracks, "Genre"}}, Dock = DockStyle.Bottom},
                new TextBox { DataBindings = {{"Text", tracks, "Rhythm"}}, Dock = DockStyle.Bottom},
                new TextBox { DataBindings = {{"Text", tracks, "Length"}}, Dock = DockStyle.Bottom},
            }
        }) {
            Application.Run(form);
        }
    }
}

与支持数据实体:

代码语言:javascript
复制
class Track : INotifyPropertyChanged {
    private string name, genre, rhythm;
    private int length;
    public event PropertyChangedEventHandler PropertyChanged;
    private void SetField<T>(ref T field, T value, string propertyName) {
        if (!EqualityComparer<T>.Default.Equals(field, value)) {
            field = value;
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public string Name { get { return name; } set { SetField(ref name, value, "Name"); } }
    public string Genre { get { return genre; } set { SetField(ref genre, value, "Genre"); } }
    public string Rhythm { get { return rhythm; } set { SetField(ref rhythm, value, "Rhythm"); } }
    public int Length { get { return length; } set { SetField(ref length, value, "Length"); } }
}
票数 2
EN

Stack Overflow用户

发布于 2009-08-08 08:44:46

试着在接下来的一段时间里对错误简历进行注释,看看会发生什么。我把自己弄糊涂了好多次了,连那句话都数不清。

编辑

我刚意识到这是VB.Net。在这种情况下,您不应该在下一个错误简历上使用。使用试着接住结构。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1248352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档